errors.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. type cause struct {
  71. cause error
  72. message string
  73. }
  74. func (c cause) Error() string { return c.Message() + ": " + c.Cause().Error() }
  75. func (c cause) Cause() error { return c.cause }
  76. func (c cause) Message() string { return c.message }
  77. // Errorf formats according to a format specifier and returns the string
  78. // as a value that satisfies error.
  79. func Errorf(format string, args ...interface{}) error {
  80. return struct {
  81. error
  82. *stack
  83. }{
  84. fmt.Errorf(format, args...),
  85. callers(),
  86. }
  87. }
  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. // type Location interface {
  156. // Location() (file string, line int)
  157. // }
  158. //
  159. // Print will also print the file and line of the error.
  160. // If err is nil, nothing is printed.
  161. //
  162. // Deprecated: Fprint will be removed in version 0.7.
  163. func Fprint(w io.Writer, err error) {
  164. type location interface {
  165. Location() (string, int)
  166. }
  167. type stacktrace interface {
  168. Stacktrace() []Frame
  169. }
  170. type message interface {
  171. Message() string
  172. }
  173. for err != nil {
  174. switch err := err.(type) {
  175. case stacktrace:
  176. frame := err.Stacktrace()[0]
  177. fmt.Fprintf(w, "%+s:%d: ", frame, frame)
  178. case location:
  179. file, line := err.Location()
  180. fmt.Fprintf(w, "%s:%d: ", file, line)
  181. default:
  182. // de nada
  183. }
  184. switch err := err.(type) {
  185. case message:
  186. fmt.Fprintln(w, err.Message())
  187. default:
  188. fmt.Fprintln(w, err.Error())
  189. }
  190. cause, ok := err.(causer)
  191. if !ok {
  192. break
  193. }
  194. err = cause.Cause()
  195. }
  196. }