errors.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Package errors implements functions for manipulating errors.
  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. // In addition, errors.Wrap records the file and line where it was called,
  25. // allowing the programmer to retrieve the path to the original error.
  26. //
  27. // Retrieving the cause of an error
  28. //
  29. // Using errors.Wrap constructs a stack of errors, adding context to the
  30. // preceeding error. Depending on the nature of the error it may be necessary
  31. // to recerse the operation of errors.Wrap to retrieve the original error
  32. // for inspection. Any error value which implements this interface
  33. //
  34. // type causer interface {
  35. // Cause() error
  36. // }
  37. //
  38. // Can be inspected by errors.Cause which will recursively retrieve the topmost
  39. // error which does nor implement causer, which is assumed to be the original
  40. // cause. For example:
  41. //
  42. // switch err := errors.Cause(err).(type) {
  43. // case *MyError:
  44. // // handle specifically
  45. // default:
  46. // // unknown error
  47. // }
  48. package errors
  49. import (
  50. "errors"
  51. "fmt"
  52. "io"
  53. "os"
  54. "runtime"
  55. "strings"
  56. )
  57. type loc uintptr
  58. func (l loc) Location() (string, int) {
  59. pc := uintptr(l)
  60. fn := runtime.FuncForPC(pc)
  61. if fn == nil {
  62. return "unknown", 0
  63. }
  64. _, prefix, _, _ := runtime.Caller(0)
  65. file, line := fn.FileLine(pc)
  66. if i := strings.LastIndex(prefix, "github.com/pkg/errors"); i > 0 {
  67. file = file[i:]
  68. }
  69. return file, line
  70. }
  71. // New returns an error that formats as the given text.
  72. func New(text string) error {
  73. pc, _, _, _ := runtime.Caller(1)
  74. return struct {
  75. error
  76. loc
  77. }{
  78. errors.New(text),
  79. loc(pc),
  80. }
  81. }
  82. type e struct {
  83. cause error
  84. message string
  85. loc
  86. }
  87. func (e *e) Error() string {
  88. return e.message + ": " + e.cause.Error()
  89. }
  90. func (e *e) Cause() error {
  91. return e.cause
  92. }
  93. // Wrap returns an error annotating the cause with message.
  94. // If cause is nil, Wrap returns nil.
  95. func Wrap(cause error, message string) error {
  96. if cause == nil {
  97. return nil
  98. }
  99. pc, _, _, _ := runtime.Caller(1)
  100. return &e{
  101. cause: cause,
  102. message: message,
  103. loc: loc(pc),
  104. }
  105. }
  106. type causer interface {
  107. Cause() error
  108. }
  109. // Cause returns the underlying cause of the error, if possible.
  110. // An error value has a cause if it implements the following
  111. // interface:
  112. //
  113. // type Causer interface {
  114. // Cause() error
  115. // }
  116. //
  117. // If the error does not implement Cause, the original error will
  118. // be returned. If the error is nil, nil will be returned without further
  119. // investigation.
  120. func Cause(err error) error {
  121. for err != nil {
  122. cause, ok := err.(causer)
  123. if !ok {
  124. break
  125. }
  126. err = cause.Cause()
  127. }
  128. return err
  129. }
  130. type locationer interface {
  131. Location() (string, int)
  132. }
  133. // Print prints the error to Stderr.
  134. // If the error implements the Causer interface described in Cause
  135. // Print will recurse into the error's cause.
  136. // If the error implements the inteface:
  137. //
  138. // type Location interface {
  139. // Location() (file string, line int)
  140. // }
  141. //
  142. // Print will also print the file and line of the error.
  143. func Print(err error) {
  144. Fprint(os.Stderr, err)
  145. }
  146. // Fprint prints the error to the supplied writer.
  147. // The format of the output is the same as Print.
  148. // If err is nil, nothing is printed.
  149. func Fprint(w io.Writer, err error) {
  150. for err != nil {
  151. location, ok := err.(locationer)
  152. if ok {
  153. file, line := location.Location()
  154. fmt.Fprintf(w, "%s:%d: ", file, line)
  155. }
  156. switch err := err.(type) {
  157. case *e:
  158. fmt.Fprintln(w, err.message)
  159. default:
  160. fmt.Fprintln(w, err.Error())
  161. }
  162. cause, ok := err.(causer)
  163. if !ok {
  164. break
  165. }
  166. err = cause.Cause()
  167. }
  168. }