error_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package errors
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "reflect"
  7. "runtime"
  8. "strings"
  9. "testing"
  10. )
  11. func BenchmarkStackFormat(b *testing.B) {
  12. b.ReportAllocs()
  13. for i := 0; i < b.N; i++ {
  14. func() {
  15. defer func() {
  16. err := recover()
  17. if err != 'a' {
  18. b.Fatal(err)
  19. }
  20. e := Errorf("hi")
  21. _ = string(e.Stack())
  22. }()
  23. a()
  24. }()
  25. }
  26. }
  27. func TestStackFormat(t *testing.T) {
  28. defer func() {
  29. err := recover()
  30. if err != 'a' {
  31. t.Fatal(err)
  32. }
  33. e, expected := Errorf("hi"), callers()
  34. bs := [][]uintptr{e.stack, expected}
  35. if err := compareStacks(bs[0], bs[1]); err != nil {
  36. t.Errorf("Stack didn't match")
  37. t.Errorf(err.Error())
  38. }
  39. stack := string(e.Stack())
  40. if !strings.Contains(stack, "a: b(5)") {
  41. t.Errorf("Stack trace does not contain source line: 'a: b(5)'")
  42. t.Errorf(stack)
  43. }
  44. if !strings.Contains(stack, "error_test.go:") {
  45. t.Errorf("Stack trace does not contain file name: 'error_test.go:'")
  46. t.Errorf(stack)
  47. }
  48. }()
  49. a()
  50. }
  51. func TestSkipWorks(t *testing.T) {
  52. defer func() {
  53. err := recover()
  54. if err != 'a' {
  55. t.Fatal(err)
  56. }
  57. bs := [][]uintptr{Wrap("hi", 2).stack, callersSkip(2)}
  58. if err := compareStacks(bs[0], bs[1]); err != nil {
  59. t.Errorf("Stack didn't match")
  60. t.Errorf(err.Error())
  61. }
  62. }()
  63. a()
  64. }
  65. func TestNew(t *testing.T) {
  66. err := New("foo")
  67. if err.Error() != "foo" {
  68. t.Errorf("Wrong message")
  69. }
  70. err = New(fmt.Errorf("foo"))
  71. if err.Error() != "foo" {
  72. t.Errorf("Wrong message")
  73. }
  74. bs := [][]uintptr{New("foo").stack, callers()}
  75. if err := compareStacks(bs[0], bs[1]); err != nil {
  76. t.Errorf("Stack didn't match")
  77. t.Errorf(err.Error())
  78. }
  79. if err.ErrorStack() != err.TypeName()+" "+err.Error()+"\n"+string(err.Stack()) {
  80. t.Errorf("ErrorStack is in the wrong format")
  81. }
  82. }
  83. // This test should work for any go version
  84. func TestIs(t *testing.T) {
  85. if Is(nil, io.EOF) {
  86. t.Errorf("nil is an error")
  87. }
  88. if !Is(io.EOF, io.EOF) {
  89. t.Errorf("io.EOF is not io.EOF")
  90. }
  91. if !Is(io.EOF, New(io.EOF)) {
  92. t.Errorf("io.EOF is not New(io.EOF)")
  93. }
  94. if !Is(New(io.EOF), New(io.EOF)) {
  95. t.Errorf("New(io.EOF) is not New(io.EOF)")
  96. }
  97. if Is(io.EOF, fmt.Errorf("io.EOF")) {
  98. t.Errorf("io.EOF is fmt.Errorf")
  99. }
  100. }
  101. func TestWrapError(t *testing.T) {
  102. e := func() error {
  103. return Wrap("hi", 1)
  104. }()
  105. if e.Error() != "hi" {
  106. t.Errorf("Constructor with a string failed")
  107. }
  108. if Wrap(fmt.Errorf("yo"), 0).Error() != "yo" {
  109. t.Errorf("Constructor with an error failed")
  110. }
  111. if Wrap(e, 0) != e {
  112. t.Errorf("Constructor with an Error failed")
  113. }
  114. if Wrap(nil, 0) != nil {
  115. t.Errorf("Constructor with nil failed")
  116. }
  117. }
  118. func TestWrapPrefixError(t *testing.T) {
  119. e := func() error {
  120. return WrapPrefix("hi", "prefix", 1)
  121. }()
  122. if e.Error() != "prefix: hi" {
  123. t.Errorf("Constructor with a string failed")
  124. }
  125. if WrapPrefix(fmt.Errorf("yo"), "prefix", 0).Error() != "prefix: yo" {
  126. t.Errorf("Constructor with an error failed")
  127. }
  128. prefixed := WrapPrefix(e, "prefix", 0)
  129. original := e.(*Error)
  130. if prefixed.Err != original.Err || !reflect.DeepEqual(prefixed.stack, original.stack) || !reflect.DeepEqual(prefixed.frames, original.frames) || prefixed.Error() != "prefix: prefix: hi" {
  131. t.Errorf("Constructor with an Error failed")
  132. }
  133. if original.Error() == prefixed.Error() {
  134. t.Errorf("WrapPrefix changed the original error")
  135. }
  136. if WrapPrefix(nil, "prefix", 0) != nil {
  137. t.Errorf("Constructor with nil failed")
  138. }
  139. if !strings.HasSuffix(original.StackFrames()[0].File, "error_test.go") || strings.HasSuffix(original.StackFrames()[1].File, "error_test.go") {
  140. t.Errorf("Skip failed")
  141. }
  142. }
  143. func ExampleErrorf(x int) (int, error) {
  144. if x%2 == 1 {
  145. return 0, Errorf("can only halve even numbers, got %d", x)
  146. }
  147. return x / 2, nil
  148. }
  149. func ExampleWrapError() (error, error) {
  150. // Wrap io.EOF with the current stack-trace and return it
  151. return nil, Wrap(io.EOF, 0)
  152. }
  153. func ExampleWrapError_skip() {
  154. defer func() {
  155. if err := recover(); err != nil {
  156. // skip 1 frame (the deferred function) and then return the wrapped err
  157. err = Wrap(err, 1)
  158. }
  159. }()
  160. }
  161. func ExampleIs(reader io.Reader, buff []byte) {
  162. _, err := reader.Read(buff)
  163. if Is(err, io.EOF) {
  164. return
  165. }
  166. }
  167. func ExampleNew(UnexpectedEOF error) error {
  168. // calling New attaches the current stacktrace to the existing UnexpectedEOF error
  169. return New(UnexpectedEOF)
  170. }
  171. func ExampleWrap() error {
  172. if err := recover(); err != nil {
  173. return Wrap(err, 1)
  174. }
  175. return a()
  176. }
  177. func ExampleError_Error(err error) {
  178. fmt.Println(err.Error())
  179. }
  180. func ExampleError_ErrorStack(err error) {
  181. fmt.Println(err.(*Error).ErrorStack())
  182. }
  183. func ExampleError_Stack(err *Error) {
  184. fmt.Println(err.Stack())
  185. }
  186. func ExampleError_TypeName(err *Error) {
  187. fmt.Println(err.TypeName(), err.Error())
  188. }
  189. func ExampleError_StackFrames(err *Error) {
  190. for _, frame := range err.StackFrames() {
  191. fmt.Println(frame.File, frame.LineNumber, frame.Package, frame.Name)
  192. }
  193. }
  194. func a() error {
  195. b(5)
  196. return nil
  197. }
  198. func b(i int) {
  199. c()
  200. }
  201. func c() {
  202. panic('a')
  203. }
  204. // compareStacks will compare a stack created using the errors package (actual)
  205. // with a reference stack created with the callers function (expected). The
  206. // first entry is not compared since the actual and expected stacks cannot
  207. // be created at the exact same program counter position so the first entry
  208. // will always differ somewhat. Returns nil if the stacks are equal enough and
  209. // an error containing a detailed error message otherwise.
  210. func compareStacks(actual, expected []uintptr) error {
  211. if len(actual) != len(expected) {
  212. return stackCompareError("Stacks does not have equal length", actual, expected)
  213. }
  214. for i, pc := range actual {
  215. if i != 0 && pc != expected[i] {
  216. return stackCompareError(fmt.Sprintf("Stacks does not match entry %d (and maybe others)", i), actual, expected)
  217. }
  218. }
  219. return nil
  220. }
  221. func stackCompareError(msg string, actual, expected []uintptr) error {
  222. return fmt.Errorf("%s\nActual stack trace:\n%s\nExpected stack trace:\n%s", msg, readableStackTrace(actual), readableStackTrace(expected))
  223. }
  224. func callers() []uintptr {
  225. return callersSkip(1)
  226. }
  227. func callersSkip(skip int) []uintptr {
  228. callers := make([]uintptr, MaxStackDepth)
  229. length := runtime.Callers(skip+2, callers[:])
  230. return callers[:length]
  231. }
  232. func readableStackTrace(callers []uintptr) string {
  233. var result bytes.Buffer
  234. frames := callersToFrames(callers)
  235. for _, frame := range frames {
  236. result.WriteString(fmt.Sprintf("%s:%d (%#x)\n\t%s\n", frame.File, frame.Line, frame.PC, frame.Function))
  237. }
  238. return result.String()
  239. }
  240. func callersToFrames(callers []uintptr) []runtime.Frame {
  241. frames := make([]runtime.Frame, 0, len(callers))
  242. framesPtr := runtime.CallersFrames(callers)
  243. for {
  244. frame, more := framesPtr.Next()
  245. frames = append(frames, frame)
  246. if !more {
  247. return frames
  248. }
  249. }
  250. }