errwrap.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Package errwrap implements methods to formalize error wrapping in Go.
  2. //
  3. // All of the top-level functions that take an `error` are built to be able
  4. // to take any error, not just wrapped errors. This allows you to use errwrap
  5. // without having to type-check and type-cast everywhere.
  6. package errwrap
  7. import (
  8. "errors"
  9. "reflect"
  10. "strings"
  11. )
  12. // WalkFunc is the callback called for Walk.
  13. type WalkFunc func(error)
  14. // Wrapper is an interface that can be implemented by custom types to
  15. // have all the Contains, Get, etc. functions in errwrap work.
  16. //
  17. // When Walk reaches a Wrapper, it will call the callback for every
  18. // wrapped error in addition to the wrapper itself. Since all the top-level
  19. // functions in errwrap use Walk, this means that all those functions work
  20. // with your custom type.
  21. type Wrapper interface {
  22. WrappedErrors() []error
  23. }
  24. // Wrap defines that outer wraps inner, returning an error type that
  25. // can be cleanly used with the other methods in this package, such as
  26. // Contains, GetAll, etc.
  27. //
  28. // This function won't modify the error message at all (the outer message
  29. // will be used).
  30. func Wrap(outer, inner error) error {
  31. return &wrappedError{
  32. Outer: outer,
  33. Inner: inner,
  34. }
  35. }
  36. // Wrapf wraps an error with a formatting message. This is similar to using
  37. // `fmt.Errorf` to wrap an error. If you're using `fmt.Errorf` to wrap
  38. // errors, you should replace it with this.
  39. //
  40. // format is the format of the error message. The string '{{err}}' will
  41. // be replaced with the original error message.
  42. func Wrapf(format string, err error) error {
  43. outerMsg := "<nil>"
  44. if err != nil {
  45. outerMsg = err.Error()
  46. }
  47. outer := errors.New(strings.Replace(
  48. format, "{{err}}", outerMsg, -1))
  49. return Wrap(outer, err)
  50. }
  51. // Contains checks if the given error contains an error with the
  52. // message msg. If err is not a wrapped error, this will always return
  53. // false unless the error itself happens to match this msg.
  54. func Contains(err error, msg string) bool {
  55. return len(GetAll(err, msg)) > 0
  56. }
  57. // ContainsType checks if the given error contains an error with
  58. // the same concrete type as v. If err is not a wrapped error, this will
  59. // check the err itself.
  60. func ContainsType(err error, v interface{}) bool {
  61. return len(GetAllType(err, v)) > 0
  62. }
  63. // Get is the same as GetAll but returns the deepest matching error.
  64. func Get(err error, msg string) error {
  65. es := GetAll(err, msg)
  66. if len(es) > 0 {
  67. return es[len(es)-1]
  68. }
  69. return nil
  70. }
  71. // GetType is the same as GetAllType but returns the deepest matching error.
  72. func GetType(err error, v interface{}) error {
  73. es := GetAllType(err, v)
  74. if len(es) > 0 {
  75. return es[len(es)-1]
  76. }
  77. return nil
  78. }
  79. // GetAll gets all the errors that might be wrapped in err with the
  80. // given message. The order of the errors is such that the outermost
  81. // matching error (the most recent wrap) is index zero, and so on.
  82. func GetAll(err error, msg string) []error {
  83. var result []error
  84. Walk(err, func(err error) {
  85. if err.Error() == msg {
  86. result = append(result, err)
  87. }
  88. })
  89. return result
  90. }
  91. // GetAllType gets all the errors that are the same type as v.
  92. //
  93. // The order of the return value is the same as described in GetAll.
  94. func GetAllType(err error, v interface{}) []error {
  95. var result []error
  96. var search string
  97. if v != nil {
  98. search = reflect.TypeOf(v).String()
  99. }
  100. Walk(err, func(err error) {
  101. var needle string
  102. if err != nil {
  103. needle = reflect.TypeOf(err).String()
  104. }
  105. if needle == search {
  106. result = append(result, err)
  107. }
  108. })
  109. return result
  110. }
  111. // Walk walks all the wrapped errors in err and calls the callback. If
  112. // err isn't a wrapped error, this will be called once for err. If err
  113. // is a wrapped error, the callback will be called for both the wrapper
  114. // that implements error as well as the wrapped error itself.
  115. func Walk(err error, cb WalkFunc) {
  116. if err == nil {
  117. return
  118. }
  119. switch e := err.(type) {
  120. case *wrappedError:
  121. cb(e.Outer)
  122. Walk(e.Inner, cb)
  123. case Wrapper:
  124. cb(err)
  125. for _, err := range e.WrappedErrors() {
  126. Walk(err, cb)
  127. }
  128. default:
  129. cb(err)
  130. }
  131. }
  132. // wrappedError is an implementation of error that has both the
  133. // outer and inner errors.
  134. type wrappedError struct {
  135. Outer error
  136. Inner error
  137. }
  138. func (w *wrappedError) Error() string {
  139. return w.Outer.Error()
  140. }
  141. func (w *wrappedError) WrappedErrors() []error {
  142. return []error{w.Outer, w.Inner}
  143. }