error.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Package errors provides errors that have stack-traces.
  2. //
  3. // This is particularly useful when you want to understand the
  4. // state of execution when an error was returned unexpectedly.
  5. //
  6. // It provides the type *Error which implements the standard
  7. // golang error interface, so you can use this library interchangably
  8. // with code that is expecting a normal error return.
  9. //
  10. // For example:
  11. //
  12. // package crashy
  13. //
  14. // import "github.com/go-errors/errors"
  15. //
  16. // var Crashed = errors.Errorf("oh dear")
  17. //
  18. // func Crash() error {
  19. // return errors.New(Crashed)
  20. // }
  21. //
  22. // This can be called as follows:
  23. //
  24. // package main
  25. //
  26. // import (
  27. // "crashy"
  28. // "fmt"
  29. // "github.com/go-errors/errors"
  30. // )
  31. //
  32. // func main() {
  33. // err := crashy.Crash()
  34. // if err != nil {
  35. // if errors.Is(err, crashy.Crashed) {
  36. // fmt.Println(err.(*errors.Error).ErrorStack())
  37. // } else {
  38. // panic(err)
  39. // }
  40. // }
  41. // }
  42. //
  43. // This package was original written to allow reporting to Bugsnag,
  44. // but after I found similar packages by Facebook and Dropbox, it
  45. // was moved to one canonical location so everyone can benefit.
  46. package errors
  47. import (
  48. "bytes"
  49. "fmt"
  50. "reflect"
  51. "runtime"
  52. )
  53. // The maximum number of stackframes on any error.
  54. var MaxStackDepth = 50
  55. // Error is an error with an attached stacktrace. It can be used
  56. // wherever the builtin error interface is expected.
  57. type Error struct {
  58. Err error
  59. stack []uintptr
  60. frames []StackFrame
  61. }
  62. // New makes an Error from the given value. If that value is already an
  63. // error then it will be used directly, if not, it will be passed to
  64. // fmt.Errorf("%v"). The stacktrace will point to the line of code that
  65. // called New.
  66. func New(e interface{}) *Error {
  67. var err error
  68. switch e := e.(type) {
  69. case error:
  70. err = e
  71. default:
  72. err = fmt.Errorf("%v", e)
  73. }
  74. stack := make([]uintptr, MaxStackDepth)
  75. length := runtime.Callers(2, stack[:])
  76. return &Error{
  77. Err: err,
  78. stack: stack[:length],
  79. }
  80. }
  81. // Wrap makes an Error from the given value. If that value is already an
  82. // error then it will be used directly, if not, it will be passed to
  83. // fmt.Errorf("%v"). The skip parameter indicates how far up the stack
  84. // to start the stacktrace. 0 is from the current call, 1 from its caller, etc.
  85. func Wrap(e interface{}, skip int) *Error {
  86. var err error
  87. switch e := e.(type) {
  88. case *Error:
  89. return e
  90. case error:
  91. err = e
  92. default:
  93. err = fmt.Errorf("%v", e)
  94. }
  95. stack := make([]uintptr, MaxStackDepth)
  96. length := runtime.Callers(2+skip, stack[:])
  97. return &Error{
  98. Err: err,
  99. stack: stack[:length],
  100. }
  101. }
  102. // Is detects whether the error is equal to a given error. Errors
  103. // are considered equal by this function if they are the same object,
  104. // or if they both contain the same error inside an errors.Error.
  105. func Is(e error, original error) bool {
  106. if e == original {
  107. return true
  108. }
  109. if e, ok := e.(*Error); ok {
  110. return Is(e.Err, original)
  111. }
  112. if original, ok := original.(*Error); ok {
  113. return Is(e, original.Err)
  114. }
  115. return false
  116. }
  117. // Errorf creates a new error with the given message. You can use it
  118. // as a drop-in replacement for fmt.Errorf() to provide descriptive
  119. // errors in return values.
  120. func Errorf(format string, a ...interface{}) *Error {
  121. return Wrap(fmt.Errorf(format, a...), 1)
  122. }
  123. // Error returns the underlying error's message.
  124. func (err *Error) Error() string {
  125. return err.Err.Error()
  126. }
  127. // Stack returns the callstack formatted the same way that go does
  128. // in runtime/debug.Stack()
  129. func (err *Error) Stack() []byte {
  130. buf := bytes.Buffer{}
  131. for _, frame := range err.StackFrames() {
  132. buf.WriteString(frame.String())
  133. }
  134. return buf.Bytes()
  135. }
  136. // ErrorStack returns a string that contains both the
  137. // error message and the callstack.
  138. func (err *Error) ErrorStack() string {
  139. return err.TypeName() + " " + err.Error() + "\n" + string(err.Stack())
  140. }
  141. // StackFrames returns an array of frames containing information about the
  142. // stack.
  143. func (err *Error) StackFrames() []StackFrame {
  144. if err.frames == nil {
  145. err.frames = make([]StackFrame, len(err.stack))
  146. for i, pc := range err.stack {
  147. err.frames[i] = NewStackFrame(pc)
  148. }
  149. }
  150. return err.frames
  151. }
  152. // TypeName returns the type this error. e.g. *errors.stringError.
  153. func (err *Error) TypeName() string {
  154. if _, ok := err.Err.(uncaughtPanic); ok {
  155. return "panic"
  156. }
  157. return reflect.TypeOf(err.Err).String()
  158. }