errors.go 891 B

123456789101112131415161718192021222324252627282930313233
  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. // Package errors implements functions to manipulate errors.
  5. package errors
  6. import (
  7. "fmt"
  8. )
  9. // New formats a string according to the format specifier and arguments and
  10. // returns an error that has a "proto" prefix.
  11. func New(f string, x ...interface{}) error {
  12. for i := 0; i < len(x); i++ {
  13. if e, ok := x[i].(*prefixError); ok {
  14. x[i] = e.s // avoid "proto: " prefix when chaining
  15. }
  16. }
  17. return &prefixError{s: fmt.Sprintf(f, x...)}
  18. }
  19. type prefixError struct{ s string }
  20. func (e *prefixError) Error() string { return "proto: " + e.s }
  21. func InvalidUTF8(name string) error {
  22. return New("field %v contains invalid UTF-8", name)
  23. }
  24. func RequiredNotSet(name string) error {
  25. return New("required field %v not set", name)
  26. }