errors.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "sort"
  9. "strings"
  10. )
  11. // TODO: This package currently only provides functionality for constructing
  12. // non-fatal errors. However, it does not currently provide functionality
  13. // to test for a specific kind of non-fatal error, which is necessary
  14. // for the end user.
  15. //
  16. // When that functionality is added, we need to think carefully about whether
  17. // a user only cares that some kind of non-fatal error was present or whether
  18. // all of the errors are of the same kind of non-fatal error.
  19. // NonFatalErrors is a list of non-fatal errors where each error
  20. // must either be a RequiredNotSet error or InvalidUTF8 error.
  21. // The list must not be empty.
  22. type NonFatalErrors []error
  23. func (es NonFatalErrors) Error() string {
  24. ms := map[string]struct{}{}
  25. for _, e := range es {
  26. ms[e.Error()] = struct{}{}
  27. }
  28. var ss []string
  29. for s := range ms {
  30. ss = append(ss, s)
  31. }
  32. sort.Strings(ss)
  33. return "proto: " + strings.Join(ss, "; ")
  34. }
  35. // NonFatal contains non-fatal errors, which are errors that permit execution
  36. // to continue, but should return with a non-nil error. As such, NonFatal is
  37. // a data structure useful for swallowing non-fatal errors, but being able to
  38. // reproduce them at the end of the function.
  39. // An error is non-fatal if it is collection of non-fatal errors, or is
  40. // an individual error where IsRequiredNotSet or IsInvalidUTF8 reports true.
  41. //
  42. // Typical usage pattern:
  43. // var nerr errors.NonFatal
  44. // ...
  45. // if err := MyFunction(); !nerr.Merge(err) {
  46. // return nil, err // immediately return if err is fatal
  47. // }
  48. // ...
  49. // return out, nerr.E
  50. type NonFatal struct{ E error }
  51. // Merge merges err into nf and reports whether it was successful.
  52. // Otherwise it returns false for any fatal non-nil errors.
  53. func (nf *NonFatal) Merge(err error) (ok bool) {
  54. if err == nil {
  55. return true // not an error
  56. }
  57. if es, ok := err.(NonFatalErrors); ok {
  58. nf.append(es...)
  59. return true // merged a list of non-fatal errors
  60. }
  61. if e, ok := err.(interface{ RequiredNotSet() bool }); ok && e.RequiredNotSet() {
  62. nf.append(err)
  63. return true // non-fatal RequiredNotSet error
  64. }
  65. if e, ok := err.(interface{ InvalidUTF8() bool }); ok && e.InvalidUTF8() {
  66. nf.append(err)
  67. return true // non-fatal InvalidUTF8 error
  68. }
  69. return false // fatal error
  70. }
  71. // AppendRequiredNotSet appends a RequiredNotSet error.
  72. func (nf *NonFatal) AppendRequiredNotSet(field string) {
  73. nf.append(requiredNotSetError(field))
  74. }
  75. // AppendInvalidUTF8 appends an InvalidUTF8 error.
  76. func (nf *NonFatal) AppendInvalidUTF8(field string) {
  77. nf.append(invalidUTF8Error(field))
  78. }
  79. func (nf *NonFatal) append(errs ...error) {
  80. es, _ := nf.E.(NonFatalErrors)
  81. es = append(es, errs...)
  82. nf.E = es
  83. }
  84. type requiredNotSetError string
  85. func (e requiredNotSetError) Error() string {
  86. if e == "" {
  87. return "required field not set"
  88. }
  89. return string("required field " + e + " not set")
  90. }
  91. func (requiredNotSetError) RequiredNotSet() bool { return true }
  92. type invalidUTF8Error string
  93. func (e invalidUTF8Error) Error() string {
  94. if e == "" {
  95. return "invalid UTF-8 detected"
  96. }
  97. return string("field " + e + " contains invalid UTF-8")
  98. }
  99. func (invalidUTF8Error) InvalidUTF8() bool { return true }
  100. // New formats a string according to the format specifier and arguments and
  101. // returns an error that has a "proto" prefix.
  102. func New(f string, x ...interface{}) error {
  103. for i := 0; i < len(x); i++ {
  104. if e, ok := x[i].(prefixError); ok {
  105. x[i] = e.s // avoid "proto: " prefix when chaining
  106. }
  107. }
  108. return &prefixError{s: fmt.Sprintf(f, x...)}
  109. }
  110. type prefixError struct{ s string }
  111. func (e *prefixError) Error() string { return "proto: " + e.s }