errors.go 5.2 KB

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