errors.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Package errors provides simple error handling primitives.
  2. //
  3. // The traditional error handling idiom in Go is roughly akin to
  4. //
  5. // if err != nil {
  6. // return err
  7. // }
  8. //
  9. // which applied recursively up the call stack results in error reports
  10. // without context or debugging information. The errors package allows
  11. // programmers to add context to the failure path in their code in a way
  12. // that does not destroy the original value of the error.
  13. //
  14. // Adding context to an error
  15. //
  16. // The errors.Wrap function returns a new error that adds context to the
  17. // original error. For example
  18. //
  19. // _, err := ioutil.ReadAll(r)
  20. // if err != nil {
  21. // return errors.Wrap(err, "read failed")
  22. // }
  23. //
  24. // Retrieving the cause of an error
  25. //
  26. // Using errors.Wrap constructs a stack of errors, adding context to the
  27. // preceding error. Depending on the nature of the error it may be necessary
  28. // to reverse the operation of errors.Wrap to retrieve the original error
  29. // for inspection. Any error value which implements this interface
  30. //
  31. // type Causer interface {
  32. // Cause() error
  33. // }
  34. //
  35. // can be inspected by errors.Cause. errors.Cause will recursively retrieve
  36. // the topmost error which does not implement causer, which is assumed to be
  37. // the original cause. For example:
  38. //
  39. // switch err := errors.Cause(err).(type) {
  40. // case *MyError:
  41. // // handle specifically
  42. // default:
  43. // // unknown error
  44. // }
  45. //
  46. // Retrieving the stack trace of an error or wrapper
  47. //
  48. // New, Errorf, Wrap, and Wrapf record a stack trace at the point they are
  49. // invoked. This information can be retrieved with the following interface.
  50. //
  51. // type Stacktrace interface {
  52. // Stacktrace() []Frame
  53. // }
  54. //
  55. // The Frame type represents a call site in the stacktrace. Frame supports
  56. // the fmt.Formatter interface that can be used for printing information about
  57. // the stacktrace of this error. For example:
  58. //
  59. // if err, ok := err.(Stacktrace); ok {
  60. // for _, f := range err.Stacktrace() {
  61. // fmt.Printf("%+s:%d", f)
  62. // }
  63. // }
  64. //
  65. // See the documentation for Frame.Format for more details.
  66. package errors
  67. import (
  68. "fmt"
  69. "io"
  70. )
  71. // _error is an error implementation returned by New and Errorf
  72. // that implements its own fmt.Formatter.
  73. type _error struct {
  74. msg string
  75. *stack
  76. }
  77. func (e _error) Error() string { return e.msg }
  78. func (e _error) Format(s fmt.State, verb rune) {
  79. switch verb {
  80. case 'v':
  81. if s.Flag('+') {
  82. fmt.Fprintf(s, "%+v: ", e.Stacktrace()[0])
  83. }
  84. fallthrough
  85. case 's':
  86. io.WriteString(s, e.msg)
  87. }
  88. }
  89. // New returns an error that formats as the given text.
  90. func New(text string) error {
  91. return _error{
  92. text,
  93. callers(),
  94. }
  95. }
  96. // Errorf formats according to a format specifier and returns the string
  97. // as a value that satisfies error.
  98. func Errorf(format string, args ...interface{}) error {
  99. return _error{
  100. fmt.Sprintf(format, args...),
  101. callers(),
  102. }
  103. }
  104. type cause struct {
  105. cause error
  106. msg string
  107. }
  108. func (c cause) Error() string { return fmt.Sprintf("%s: %v", c.msg, c.Cause()) }
  109. func (c cause) Cause() error { return c.cause }
  110. // wrapper is an error implementation returned by Wrap and Wrapf
  111. // that implements its own fmt.Formatter.
  112. type wrapper struct {
  113. cause
  114. *stack
  115. }
  116. func (w wrapper) Format(s fmt.State, verb rune) {
  117. switch verb {
  118. case 'v':
  119. if s.Flag('+') {
  120. fmt.Fprintf(s, "%+v\n", w.Cause())
  121. fmt.Fprintf(s, "%+v: %s", w.Stacktrace()[0], w.msg)
  122. return
  123. }
  124. fallthrough
  125. case 's':
  126. io.WriteString(s, w.Error())
  127. }
  128. }
  129. // Wrap returns an error annotating err with message.
  130. // If err is nil, Wrap returns nil.
  131. func Wrap(err error, message string) error {
  132. if err == nil {
  133. return nil
  134. }
  135. return wrapper{
  136. cause: cause{
  137. cause: err,
  138. msg: message,
  139. },
  140. stack: callers(),
  141. }
  142. }
  143. // Wrapf returns an error annotating err with the format specifier.
  144. // If err is nil, Wrapf returns nil.
  145. func Wrapf(err error, format string, args ...interface{}) error {
  146. if err == nil {
  147. return nil
  148. }
  149. return wrapper{
  150. cause: cause{
  151. cause: err,
  152. msg: fmt.Sprintf(format, args...),
  153. },
  154. stack: callers(),
  155. }
  156. }
  157. // Cause returns the underlying cause of the error, if possible.
  158. // An error value has a cause if it implements the following
  159. // interface:
  160. //
  161. // type Causer interface {
  162. // Cause() error
  163. // }
  164. //
  165. // If the error does not implement Cause, the original error will
  166. // be returned. If the error is nil, nil will be returned without further
  167. // investigation.
  168. func Cause(err error) error {
  169. type causer interface {
  170. Cause() error
  171. }
  172. for err != nil {
  173. cause, ok := err.(causer)
  174. if !ok {
  175. break
  176. }
  177. err = cause.Cause()
  178. }
  179. return err
  180. }