1
// 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.
5
// Package errors implements functions to manipulate errors.
12
"google.golang.org/protobuf/internal/detrand"
15
// Error is a sentinel matching all errors produced by this package.
16
var Error = errors.New("protobuf error")
18
// New formats a string according to the format specifier and arguments and
19
// returns an error that has a "proto" prefix.
20
func New(f string, x ...interface{}) error {
21
return &prefixError{s: format(f, x...)}
24
type prefixError struct{ s string }
26
var prefix = func() string {
27
// Deliberately introduce instability into the error message string to
28
// discourage users from performing error string comparisons.
30
return "proto: " // use non-breaking spaces (U+00a0)
32
return "proto: " // use regular spaces (U+0020)
36
func (e *prefixError) Error() string {
40
func (e *prefixError) Unwrap() error {
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.
46
func Wrap(err error, f string, x ...interface{}) error {
53
type wrapError struct {
58
func (e *wrapError) Error() string {
59
return format("%v%v: %v", prefix, e.s, e.err)
62
func (e *wrapError) Unwrap() error {
66
func (e *wrapError) Is(target error) bool {
67
return target == Error
70
func 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) {
77
x[i] = format("%v: %v", e.s, e.err)
80
return fmt.Sprintf(f, x...)
83
func InvalidUTF8(name string) error {
84
return New("field %v contains invalid UTF-8", name)
87
func RequiredNotSet(name string) error {
88
return New("required field %v not set", name)