errors.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Package errors implements functions for manipulating errors.
  2. package errors
  3. import (
  4. "fmt"
  5. "io"
  6. "os"
  7. "runtime"
  8. "strings"
  9. )
  10. type loc uintptr
  11. func (l loc) Location() (string, int) {
  12. pc := uintptr(l)
  13. fn := runtime.FuncForPC(pc)
  14. if fn == nil {
  15. return "unknown", 0
  16. }
  17. _, prefix, _, _ := runtime.Caller(0)
  18. file, line := fn.FileLine(pc)
  19. if i := strings.LastIndex(prefix, "github.com/pkg/errors"); i > 0 {
  20. file = file[i:]
  21. }
  22. return file, line
  23. }
  24. // New returns an error that formats as the given text.
  25. func New(text string) error {
  26. pc, _, _, _ := runtime.Caller(1)
  27. return struct {
  28. error
  29. loc
  30. }{
  31. fmt.Errorf(text),
  32. loc(pc),
  33. }
  34. }
  35. type e struct {
  36. cause error
  37. message string
  38. loc
  39. }
  40. func (e *e) Error() string {
  41. return e.message + ": " + e.cause.Error()
  42. }
  43. func (e *e) Cause() error {
  44. return e.cause
  45. }
  46. // Wrap returns an error annotating the cause with message.
  47. // If cause is nil, Wrap returns nil.
  48. func Wrap(cause error, message string) error {
  49. if cause == nil {
  50. return nil
  51. }
  52. pc, _, _, _ := runtime.Caller(1)
  53. return &e{
  54. cause: cause,
  55. message: message,
  56. loc: loc(pc),
  57. }
  58. }
  59. type causer interface {
  60. Cause() error
  61. }
  62. // Cause returns the underlying cause of the error, if possible.
  63. // An error value has a cause if it implements the following
  64. // interface:
  65. //
  66. // type Causer interface {
  67. // Cause() error
  68. // }
  69. //
  70. // If the error does not implement Cause, the original error will
  71. // be returned. If the error is nil, nil will be returned without further
  72. // investigation.
  73. func Cause(err error) error {
  74. for err != nil {
  75. cause, ok := err.(causer)
  76. if !ok {
  77. break
  78. }
  79. err = cause.Cause()
  80. }
  81. return err
  82. }
  83. type locationer interface {
  84. Location() (string, int)
  85. }
  86. // Print prints the error to Stderr.
  87. // If the error implements the Causer interface described in Cause
  88. // Print will recurse into the error's cause.
  89. // If the error implements the inteface:
  90. //
  91. // type Location interface {
  92. // Location() (file string, line int)
  93. // }
  94. //
  95. // Print will also print the file and line of the error.
  96. func Print(err error) {
  97. Fprint(os.Stderr, err)
  98. }
  99. // Fprint prints the error to the supplied writer.
  100. // The format of the output is the same as Print.
  101. // If err is nil, nothing is printed.
  102. func Fprint(w io.Writer, err error) {
  103. for err != nil {
  104. location, ok := err.(locationer)
  105. if ok {
  106. file, line := location.Location()
  107. fmt.Fprintf(w, "%s:%d: ", file, line)
  108. }
  109. switch err := err.(type) {
  110. case *e:
  111. fmt.Fprintln(w, err.message)
  112. default:
  113. fmt.Fprintln(w, err.Error())
  114. }
  115. cause, ok := err.(causer)
  116. if !ok {
  117. break
  118. }
  119. err = cause.Cause()
  120. }
  121. }