errors.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. io.WriteString(s, e.msg)
  87. fmt.Fprintf(s, "%+v", e.Stacktrace())
  88. return
  89. }
  90. fallthrough
  91. case 's':
  92. io.WriteString(s, e.msg)
  93. }
  94. }
  95. // New returns an error that formats as the given text.
  96. func New(text string) error {
  97. return _error{
  98. text,
  99. callers(),
  100. }
  101. }
  102. // Errorf formats according to a format specifier and returns the string
  103. // as a value that satisfies error.
  104. func Errorf(format string, args ...interface{}) error {
  105. return _error{
  106. fmt.Sprintf(format, args...),
  107. callers(),
  108. }
  109. }
  110. type cause struct {
  111. cause error
  112. msg string
  113. }
  114. func (c cause) Error() string { return fmt.Sprintf("%s: %v", c.msg, c.Cause()) }
  115. func (c cause) Cause() error { return c.cause }
  116. // wrapper is an error implementation returned by Wrap and Wrapf
  117. // that implements its own fmt.Formatter.
  118. type wrapper struct {
  119. cause
  120. *stack
  121. }
  122. func (w wrapper) Format(s fmt.State, verb rune) {
  123. switch verb {
  124. case 'v':
  125. if s.Flag('+') {
  126. fmt.Fprintf(s, "%+v\n", w.Cause())
  127. fmt.Fprintf(s, "%+v: %s", w.Stacktrace()[0], w.msg)
  128. return
  129. }
  130. fallthrough
  131. case 's':
  132. io.WriteString(s, w.Error())
  133. }
  134. }
  135. // Wrap returns an error annotating err with message.
  136. // If err is nil, Wrap returns nil.
  137. func Wrap(err error, message string) error {
  138. if err == nil {
  139. return nil
  140. }
  141. return wrapper{
  142. cause: cause{
  143. cause: err,
  144. msg: message,
  145. },
  146. stack: callers(),
  147. }
  148. }
  149. // Wrapf returns an error annotating err with the format specifier.
  150. // If err is nil, Wrapf returns nil.
  151. func Wrapf(err error, format string, args ...interface{}) error {
  152. if err == nil {
  153. return nil
  154. }
  155. return wrapper{
  156. cause: cause{
  157. cause: err,
  158. msg: fmt.Sprintf(format, args...),
  159. },
  160. stack: callers(),
  161. }
  162. }
  163. // Cause returns the underlying cause of the error, if possible.
  164. // An error value has a cause if it implements the following
  165. // interface:
  166. //
  167. // type Causer interface {
  168. // Cause() error
  169. // }
  170. //
  171. // If the error does not implement Cause, the original error will
  172. // be returned. If the error is nil, nil will be returned without further
  173. // investigation.
  174. func Cause(err error) error {
  175. type causer interface {
  176. Cause() error
  177. }
  178. for err != nil {
  179. cause, ok := err.(causer)
  180. if !ok {
  181. break
  182. }
  183. err = cause.Cause()
  184. }
  185. return err
  186. }