errors.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\n", w.Cause())
  109. fmt.Fprintf(s, "%+v: %s", w.Stacktrace()[0], w.msg)
  110. return
  111. }
  112. fallthrough
  113. case 's':
  114. io.WriteString(s, w.Error())
  115. }
  116. }
  117. // Wrap returns an error annotating err with message.
  118. // If err is nil, Wrap returns nil.
  119. func Wrap(err error, message string) error {
  120. if err == nil {
  121. return nil
  122. }
  123. return wrapper{
  124. cause: cause{
  125. cause: err,
  126. msg: message,
  127. },
  128. stack: callers(),
  129. }
  130. }
  131. // Wrapf returns an error annotating err with the format specifier.
  132. // If err is nil, Wrapf returns nil.
  133. func Wrapf(err error, format string, args ...interface{}) error {
  134. if err == nil {
  135. return nil
  136. }
  137. return wrapper{
  138. cause: cause{
  139. cause: err,
  140. msg: fmt.Sprintf(format, args...),
  141. },
  142. stack: callers(),
  143. }
  144. }
  145. // Cause returns the underlying cause of the error, if possible.
  146. // An error value has a cause if it implements the following
  147. // interface:
  148. //
  149. // type Causer interface {
  150. // Cause() error
  151. // }
  152. //
  153. // If the error does not implement Cause, the original error will
  154. // be returned. If the error is nil, nil will be returned without further
  155. // investigation.
  156. func Cause(err error) error {
  157. type causer interface {
  158. Cause() error
  159. }
  160. for err != nil {
  161. cause, ok := err.(causer)
  162. if !ok {
  163. break
  164. }
  165. err = cause.Cause()
  166. }
  167. return err
  168. }
  169. // Fprint prints the error to the supplied writer.
  170. // If the error implements the Causer interface described in Cause
  171. // Fprint will recurse into the error's cause.
  172. // Fprint will also print the file and line of the error.
  173. // If err is nil, nothing is printed.
  174. //
  175. // Deprecated: Fprint will be removed in version 0.7.
  176. func Fprint(w io.Writer, err error) {
  177. if err == nil {
  178. return
  179. }
  180. fmt.Fprintf(w, "%+v\n", err)
  181. }