errors.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 stackTracer 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 stack trace. Frame supports
  71. // the fmt.Formatter interface that can be used for printing information about
  72. // the stack trace of this error. For example:
  73. //
  74. // if err, ok := err.(stackTracer); 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. io.WriteString(s, w.msg)
  139. fmt.Fprintf(s, "%+v", w.StackTrace())
  140. return
  141. }
  142. fallthrough
  143. case 's':
  144. io.WriteString(s, w.Error())
  145. case 'q':
  146. fmt.Fprintf(s, "%q", w.Error())
  147. }
  148. }
  149. // Wrap returns an error annotating err with message.
  150. // If err is nil, Wrap returns nil.
  151. func Wrap(err error, message string) error {
  152. if err == nil {
  153. return nil
  154. }
  155. return wrapper{
  156. cause: cause{
  157. cause: err,
  158. msg: message,
  159. },
  160. stack: callers(),
  161. }
  162. }
  163. // Wrapf returns an error annotating err with the format specifier.
  164. // If err is nil, Wrapf returns nil.
  165. func Wrapf(err error, format string, args ...interface{}) error {
  166. if err == nil {
  167. return nil
  168. }
  169. return wrapper{
  170. cause: cause{
  171. cause: err,
  172. msg: fmt.Sprintf(format, args...),
  173. },
  174. stack: callers(),
  175. }
  176. }
  177. // Cause returns the underlying cause of the error, if possible.
  178. // An error value has a cause if it implements the following
  179. // interface:
  180. //
  181. // type Causer interface {
  182. // Cause() error
  183. // }
  184. //
  185. // If the error does not implement Cause, the original error will
  186. // be returned. If the error is nil, nil will be returned without further
  187. // investigation.
  188. func Cause(err error) error {
  189. type causer interface {
  190. Cause() error
  191. }
  192. for err != nil {
  193. cause, ok := err.(causer)
  194. if !ok {
  195. break
  196. }
  197. err = cause.Cause()
  198. }
  199. return err
  200. }