blob: 20c17b35e3a81c7110f18b5170b5f8dadeb435b8 [file] [log] [blame]
Joe Tsai9834a7d2018-08-01 13:16:49 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Package errors implements functions to manipulate errors.
6package errors
7
8import (
Damien Neil01b51b42020-01-17 13:40:51 -08009 "errors"
Joe Tsai9834a7d2018-08-01 13:16:49 -070010 "fmt"
Joe Tsai09217f02019-09-06 16:57:46 -070011
12 "google.golang.org/protobuf/internal/detrand"
Joe Tsai9834a7d2018-08-01 13:16:49 -070013)
14
Damien Neil01b51b42020-01-17 13:40:51 -080015// Error is a sentinel matching all errors produced by this package.
16var Error = errors.New("protobuf error")
17
Joe Tsai9834a7d2018-08-01 13:16:49 -070018// New formats a string according to the format specifier and arguments and
19// returns an error that has a "proto" prefix.
20func New(f string, x ...interface{}) error {
Damien Neil01b51b42020-01-17 13:40:51 -080021 return &prefixError{s: format(f, x...)}
Joe Tsai9834a7d2018-08-01 13:16:49 -070022}
23
24type prefixError struct{ s string }
25
Joe Tsai09217f02019-09-06 16:57:46 -070026var prefix = func() string {
Joe Tsai27af11f2019-09-07 12:06:05 -070027 // Deliberately introduce instability into the error message string to
28 // discourage users from performing error string comparisons.
Joe Tsai09217f02019-09-06 16:57:46 -070029 if detrand.Bool() {
Joe Tsai27af11f2019-09-07 12:06:05 -070030 return "proto: " // use non-breaking spaces (U+00a0)
31 } else {
32 return "proto: " // use regular spaces (U+0020)
Joe Tsai09217f02019-09-06 16:57:46 -070033 }
Joe Tsai09217f02019-09-06 16:57:46 -070034}()
35
36func (e *prefixError) Error() string {
37 return prefix + e.s
38}
Damien Neil8c86fc52019-06-19 09:28:29 -070039
Damien Neil01b51b42020-01-17 13:40:51 -080040func (e *prefixError) Unwrap() error {
41 return Error
42}
43
44// Wrap returns an error that has a "proto" prefix, the formatted string described
45// by the format specifier and arguments, and a suffix of err. The error wraps err.
46func Wrap(err error, f string, x ...interface{}) error {
47 return &wrapError{
48 s: format(f, x...),
49 err: err,
50 }
51}
52
53type wrapError struct {
54 s string
55 err error
56}
57
58func (e *wrapError) Error() string {
59 return format("%v%v: %v", prefix, e.s, e.err)
60}
61
62func (e *wrapError) Unwrap() error {
63 return e.err
64}
65
66func (e *wrapError) Is(target error) bool {
67 return target == Error
68}
69
70func format(f string, x ...interface{}) string {
71 // avoid "proto: " prefix when chaining
72 for i := 0; i < len(x); i++ {
73 switch e := x[i].(type) {
74 case *prefixError:
75 x[i] = e.s
76 case *wrapError:
77 x[i] = format("%v: %v", e.s, e.err)
78 }
79 }
80 return fmt.Sprintf(f, x...)
81}
82
Damien Neil8c86fc52019-06-19 09:28:29 -070083func InvalidUTF8(name string) error {
84 return New("field %v contains invalid UTF-8", name)
85}
86
87func RequiredNotSet(name string) error {
88 return New("required field %v not set", name)
89}