errors.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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() errors.Stacktrace
  53. // }
  54. //
  55. // Where errors.Stacktrace is defined as
  56. //
  57. // type Stacktrace []Frame
  58. //
  59. // The Frame type represents a call site in the stacktrace. Frame supports
  60. // the fmt.Formatter interface that can be used for printing information about
  61. // the stacktrace of this error. For example:
  62. //
  63. // if err, ok := err.(Stacktrace); ok {
  64. // for _, f := range err.Stacktrace() {
  65. // fmt.Printf("%+s:%d", f)
  66. // }
  67. // }
  68. //
  69. // See the documentation for Frame.Format for more details.
  70. package errors
  71. import (
  72. "fmt"
  73. "io"
  74. )
  75. // _error is an error implementation returned by New and Errorf
  76. // that implements its own fmt.Formatter.
  77. type _error struct {
  78. msg string
  79. *stack
  80. }
  81. func (e _error) Error() string { return e.msg }
  82. func (e _error) Format(s fmt.State, verb rune) {
  83. switch verb {
  84. case 'v':
  85. if s.Flag('+') {
  86. fmt.Fprintf(s, "%+v: ", e.Stacktrace()[0])
  87. }
  88. fallthrough
  89. case 's':
  90. io.WriteString(s, e.msg)
  91. }
  92. }
  93. // New returns an error that formats as the given text.
  94. func New(text string) error {
  95. return _error{
  96. text,
  97. callers(),
  98. }
  99. }
  100. // Errorf formats according to a format specifier and returns the string
  101. // as a value that satisfies error.
  102. func Errorf(format string, args ...interface{}) error {
  103. return _error{
  104. fmt.Sprintf(format, args...),
  105. callers(),
  106. }
  107. }
  108. type cause struct {
  109. cause error
  110. msg string
  111. }
  112. func (c cause) Error() string { return fmt.Sprintf("%s: %v", c.msg, c.Cause()) }
  113. func (c cause) Cause() error { return c.cause }
  114. // wrapper is an error implementation returned by Wrap and Wrapf
  115. // that implements its own fmt.Formatter.
  116. type wrapper struct {
  117. cause
  118. *stack
  119. }
  120. func (w wrapper) Format(s fmt.State, verb rune) {
  121. switch verb {
  122. case 'v':
  123. if s.Flag('+') {
  124. fmt.Fprintf(s, "%+v\n", w.Cause())
  125. fmt.Fprintf(s, "%+v: %s", w.Stacktrace()[0], w.msg)
  126. return
  127. }
  128. fallthrough
  129. case 's':
  130. io.WriteString(s, w.Error())
  131. }
  132. }
  133. // Wrap returns an error annotating err with message.
  134. // If err is nil, Wrap returns nil.
  135. func Wrap(err error, message string) error {
  136. if err == nil {
  137. return nil
  138. }
  139. return wrapper{
  140. cause: cause{
  141. cause: err,
  142. msg: message,
  143. },
  144. stack: callers(),
  145. }
  146. }
  147. // Wrapf returns an error annotating err with the format specifier.
  148. // If err is nil, Wrapf returns nil.
  149. func Wrapf(err error, format string, args ...interface{}) error {
  150. if err == nil {
  151. return nil
  152. }
  153. return wrapper{
  154. cause: cause{
  155. cause: err,
  156. msg: fmt.Sprintf(format, args...),
  157. },
  158. stack: callers(),
  159. }
  160. }
  161. // Cause returns the underlying cause of the error, if possible.
  162. // An error value has a cause if it implements the following
  163. // interface:
  164. //
  165. // type Causer interface {
  166. // Cause() error
  167. // }
  168. //
  169. // If the error does not implement Cause, the original error will
  170. // be returned. If the error is nil, nil will be returned without further
  171. // investigation.
  172. func Cause(err error) error {
  173. type causer interface {
  174. Cause() error
  175. }
  176. for err != nil {
  177. cause, ok := err.(causer)
  178. if !ok {
  179. break
  180. }
  181. err = cause.Cause()
  182. }
  183. return err
  184. }