errors.go 4.7 KB

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