Joe Tsai | 9834a7d | 2018-08-01 13:16:49 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | // Package errors implements functions to manipulate errors. |
| 6 | package errors |
| 7 | |
| 8 | import ( |
| 9 | "fmt" |
Joe Tsai | 9834a7d | 2018-08-01 13:16:49 -0700 | [diff] [blame] | 10 | ) |
| 11 | |
Joe Tsai | 9834a7d | 2018-08-01 13:16:49 -0700 | [diff] [blame] | 12 | // New formats a string according to the format specifier and arguments and |
| 13 | // returns an error that has a "proto" prefix. |
| 14 | func New(f string, x ...interface{}) error { |
| 15 | for i := 0; i < len(x); i++ { |
Herbie Ong | e09920e | 2019-02-27 13:37:06 -0800 | [diff] [blame] | 16 | if e, ok := x[i].(*prefixError); ok { |
Joe Tsai | 9834a7d | 2018-08-01 13:16:49 -0700 | [diff] [blame] | 17 | x[i] = e.s // avoid "proto: " prefix when chaining |
| 18 | } |
| 19 | } |
| 20 | return &prefixError{s: fmt.Sprintf(f, x...)} |
| 21 | } |
| 22 | |
| 23 | type prefixError struct{ s string } |
| 24 | |
| 25 | func (e *prefixError) Error() string { return "proto: " + e.s } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 26 | |
| 27 | func InvalidUTF8(name string) error { |
| 28 | return New("field %v contains invalid UTF-8", name) |
| 29 | } |
| 30 | |
| 31 | func RequiredNotSet(name string) error { |
| 32 | return New("required field %v not set", name) |
| 33 | } |