error_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. func TestIs(t *testing.T) {
  84. if Is(nil, io.EOF) {
  85. t.Errorf("nil is an error")
  86. }
  87. if !Is(io.EOF, io.EOF) {
  88. t.Errorf("io.EOF is not io.EOF")
  89. }
  90. if !Is(io.EOF, New(io.EOF)) {
  91. t.Errorf("io.EOF is not New(io.EOF)")
  92. }
  93. if !Is(New(io.EOF), New(io.EOF)) {
  94. t.Errorf("New(io.EOF) is not New(io.EOF)")
  95. }
  96. if Is(io.EOF, fmt.Errorf("io.EOF")) {
  97. t.Errorf("io.EOF is fmt.Errorf")
  98. }
  99. }
  100. func TestWrapError(t *testing.T) {
  101. e := func() error {
  102. return Wrap("hi", 1)
  103. }()
  104. if e.Error() != "hi" {
  105. t.Errorf("Constructor with a string failed")
  106. }
  107. if Wrap(fmt.Errorf("yo"), 0).Error() != "yo" {
  108. t.Errorf("Constructor with an error failed")
  109. }
  110. if Wrap(e, 0) != e {
  111. t.Errorf("Constructor with an Error failed")
  112. }
  113. if Wrap(nil, 0) != nil {
  114. t.Errorf("Constructor with nil failed")
  115. }
  116. }
  117. func TestWrapPrefixError(t *testing.T) {
  118. e := func() error {
  119. return WrapPrefix("hi", "prefix", 1)
  120. }()
  121. if e.Error() != "prefix: hi" {
  122. t.Errorf("Constructor with a string failed")
  123. }
  124. if WrapPrefix(fmt.Errorf("yo"), "prefix", 0).Error() != "prefix: yo" {
  125. t.Errorf("Constructor with an error failed")
  126. }
  127. prefixed := WrapPrefix(e, "prefix", 0)
  128. original := e.(*Error)
  129. if prefixed.Err != original.Err || !reflect.DeepEqual(prefixed.stack, original.stack) || !reflect.DeepEqual(prefixed.frames, original.frames) || prefixed.Error() != "prefix: prefix: hi" {
  130. t.Errorf("Constructor with an Error failed")
  131. }
  132. if original.Error() == prefixed.Error() {
  133. t.Errorf("WrapPrefix changed the original error")
  134. }
  135. if WrapPrefix(nil, "prefix", 0) != nil {
  136. t.Errorf("Constructor with nil failed")
  137. }
  138. if !strings.HasSuffix(original.StackFrames()[0].File, "error_test.go") || strings.HasSuffix(original.StackFrames()[1].File, "error_test.go") {
  139. t.Errorf("Skip failed")
  140. }
  141. }
  142. func ExampleErrorf(x int) (int, error) {
  143. if x%2 == 1 {
  144. return 0, Errorf("can only halve even numbers, got %d", x)
  145. }
  146. return x / 2, nil
  147. }
  148. func ExampleWrapError() (error, error) {
  149. // Wrap io.EOF with the current stack-trace and return it
  150. return nil, Wrap(io.EOF, 0)
  151. }
  152. func ExampleWrapError_skip() {
  153. defer func() {
  154. if err := recover(); err != nil {
  155. // skip 1 frame (the deferred function) and then return the wrapped err
  156. err = Wrap(err, 1)
  157. }
  158. }()
  159. }
  160. func ExampleIs(reader io.Reader, buff []byte) {
  161. _, err := reader.Read(buff)
  162. if Is(err, io.EOF) {
  163. return
  164. }
  165. }
  166. func ExampleNew(UnexpectedEOF error) error {
  167. // calling New attaches the current stacktrace to the existing UnexpectedEOF error
  168. return New(UnexpectedEOF)
  169. }
  170. func ExampleWrap() error {
  171. if err := recover(); err != nil {
  172. return Wrap(err, 1)
  173. }
  174. return a()
  175. }
  176. func ExampleError_Error(err error) {
  177. fmt.Println(err.Error())
  178. }
  179. func ExampleError_ErrorStack(err error) {
  180. fmt.Println(err.(*Error).ErrorStack())
  181. }
  182. func ExampleError_Stack(err *Error) {
  183. fmt.Println(err.Stack())
  184. }
  185. func ExampleError_TypeName(err *Error) {
  186. fmt.Println(err.TypeName(), err.Error())
  187. }
  188. func ExampleError_StackFrames(err *Error) {
  189. for _, frame := range err.StackFrames() {
  190. fmt.Println(frame.File, frame.LineNumber, frame.Package, frame.Name)
  191. }
  192. }
  193. func a() error {
  194. b(5)
  195. return nil
  196. }
  197. func b(i int) {
  198. c()
  199. }
  200. func c() {
  201. panic('a')
  202. }
  203. // compareStacks will compare a stack created using the errors package (actual)
  204. // with a reference stack created with the callers function (expected). The
  205. // first entry is not compared since the actual and expected stacks cannot
  206. // be created at the exact same program counter position so the first entry
  207. // will always differ somewhat. Returns nil if the stacks are equal enough and
  208. // an error containing a detailed error message otherwise.
  209. func compareStacks(actual, expected []uintptr) error {
  210. if len(actual) != len(expected) {
  211. return stackCompareError("Stacks does not have equal length", actual, expected)
  212. }
  213. for i, pc := range actual {
  214. if i != 0 && pc != expected[i] {
  215. return stackCompareError(fmt.Sprintf("Stacks does not match entry %d (and maybe others)", i), actual, expected)
  216. }
  217. }
  218. return nil
  219. }
  220. func stackCompareError(msg string, actual, expected []uintptr) error {
  221. return fmt.Errorf("%s\nActual stack trace:\n%s\nExpected stack trace:\n%s", msg, readableStackTrace(actual), readableStackTrace(expected))
  222. }
  223. func callers() []uintptr {
  224. return callersSkip(1)
  225. }
  226. func callersSkip(skip int) []uintptr {
  227. callers := make([]uintptr, MaxStackDepth)
  228. length := runtime.Callers(skip+2, callers[:])
  229. return callers[:length]
  230. }
  231. func readableStackTrace(callers []uintptr) string {
  232. var result bytes.Buffer
  233. frames := callersToFrames(callers)
  234. for _, frame := range frames {
  235. result.WriteString(fmt.Sprintf("%s:%d (%#x)\n\t%s\n", frame.File, frame.Line, frame.PC, frame.Function))
  236. }
  237. return result.String()
  238. }
  239. func callersToFrames(callers []uintptr) []runtime.Frame {
  240. frames := make([]runtime.Frame, 0, len(callers))
  241. framesPtr := runtime.CallersFrames(callers)
  242. for {
  243. frame, more := framesPtr.Next()
  244. frames = append(frames, frame)
  245. if !more {
  246. return frames
  247. }
  248. }
  249. }