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