errors.go 4.5 KB

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