error_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package errors
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "reflect"
  7. "runtime/debug"
  8. "testing"
  9. )
  10. func TestStackFormatMatches(t *testing.T) {
  11. defer func() {
  12. err := recover()
  13. if err != 'a' {
  14. t.Fatal(err)
  15. }
  16. bs := [][]byte{Errorf("hi").Stack(), debug.Stack()}
  17. // Ignore the first line (as it contains the PC of the .Stack() call)
  18. bs[0] = bytes.SplitN(bs[0], []byte("\n"), 2)[1]
  19. bs[1] = bytes.SplitN(bs[1], []byte("\n"), 2)[1]
  20. if bytes.Compare(bs[0], bs[1]) != 0 {
  21. t.Errorf("Stack didn't match")
  22. t.Errorf("%s", bs[0])
  23. t.Errorf("%s", bs[1])
  24. }
  25. }()
  26. a()
  27. }
  28. func TestSkipWorks(t *testing.T) {
  29. defer func() {
  30. err := recover()
  31. if err != 'a' {
  32. t.Fatal(err)
  33. }
  34. bs := [][]byte{Wrap("hi", 2).Stack(), debug.Stack()}
  35. // should skip four lines of debug.Stack()
  36. bs[1] = bytes.SplitN(bs[1], []byte("\n"), 5)[4]
  37. if bytes.Compare(bs[0], bs[1]) != 0 {
  38. t.Errorf("Stack didn't match")
  39. t.Errorf("%s", bs[0])
  40. t.Errorf("%s", bs[1])
  41. }
  42. }()
  43. a()
  44. }
  45. func TestNew(t *testing.T) {
  46. err := New("foo")
  47. if err.Error() != "foo" {
  48. t.Errorf("Wrong message")
  49. }
  50. err = New(fmt.Errorf("foo"))
  51. if err.Error() != "foo" {
  52. t.Errorf("Wrong message")
  53. }
  54. bs := [][]byte{New("foo").Stack(), debug.Stack()}
  55. // Ignore the first line (as it contains the PC of the .Stack() call)
  56. bs[0] = bytes.SplitN(bs[0], []byte("\n"), 2)[1]
  57. bs[1] = bytes.SplitN(bs[1], []byte("\n"), 2)[1]
  58. if bytes.Compare(bs[0], bs[1]) != 0 {
  59. t.Errorf("Stack didn't match")
  60. t.Errorf("%s", bs[0])
  61. t.Errorf("%s", bs[1])
  62. }
  63. if err.ErrorStack() != err.TypeName()+" "+err.Error()+"\n"+string(err.Stack()) {
  64. t.Errorf("ErrorStack is in the wrong format")
  65. }
  66. }
  67. func TestIs(t *testing.T) {
  68. if Is(nil, io.EOF) {
  69. t.Errorf("nil is an error")
  70. }
  71. if !Is(io.EOF, io.EOF) {
  72. t.Errorf("io.EOF is not io.EOF")
  73. }
  74. if !Is(io.EOF, New(io.EOF)) {
  75. t.Errorf("io.EOF is not New(io.EOF)")
  76. }
  77. if !Is(New(io.EOF), New(io.EOF)) {
  78. t.Errorf("New(io.EOF) is not New(io.EOF)")
  79. }
  80. if Is(io.EOF, fmt.Errorf("io.EOF")) {
  81. t.Errorf("io.EOF is fmt.Errorf")
  82. }
  83. }
  84. func TestWrapError(t *testing.T) {
  85. e := func() error {
  86. return Wrap("hi", 1)
  87. }()
  88. if e.Error() != "hi" {
  89. t.Errorf("Constructor with a string failed")
  90. }
  91. if Wrap(fmt.Errorf("yo"), 0).Error() != "yo" {
  92. t.Errorf("Constructor with an error failed")
  93. }
  94. if Wrap(e, 0) != e {
  95. t.Errorf("Constructor with an Error failed")
  96. }
  97. if Wrap(nil, 0).Error() != "<nil>" {
  98. t.Errorf("Constructor with nil failed")
  99. }
  100. }
  101. func TestWrapPrefixError(t *testing.T) {
  102. e := func() error {
  103. return WrapPrefix("hi", "prefix", 1)
  104. }()
  105. fmt.Println(e.Error())
  106. if e.Error() != "prefix: hi" {
  107. t.Errorf("Constructor with a string failed")
  108. }
  109. if WrapPrefix(fmt.Errorf("yo"), "prefix", 0).Error() != "prefix: yo" {
  110. t.Errorf("Constructor with an error failed")
  111. }
  112. prefixed := WrapPrefix(e, "prefix", 0)
  113. original := e.(*Error)
  114. if prefixed.Err != original.Err || !reflect.DeepEqual(prefixed.stack, original.stack) || !reflect.DeepEqual(prefixed.frames, original.frames) || prefixed.Error() != "prefix: prefix: hi" {
  115. t.Errorf("Constructor with an Error failed")
  116. }
  117. if original.Error() == prefixed.Error() {
  118. t.Errorf("WrapPrefix changed the original error")
  119. }
  120. if WrapPrefix(nil, "prefix", 0).Error() != "prefix: <nil>" {
  121. t.Errorf("Constructor with nil failed")
  122. }
  123. }
  124. func ExampleErrorf(x int) (int, error) {
  125. if x%2 == 1 {
  126. return 0, Errorf("can only halve even numbers, got %d", x)
  127. }
  128. return x / 2, nil
  129. }
  130. func ExampleWrapError() (error, error) {
  131. // Wrap io.EOF with the current stack-trace and return it
  132. return nil, Wrap(io.EOF, 0)
  133. }
  134. func ExampleWrapError_skip() {
  135. defer func() {
  136. if err := recover(); err != nil {
  137. // skip 1 frame (the deferred function) and then return the wrapped err
  138. err = Wrap(err, 1)
  139. }
  140. }()
  141. }
  142. func ExampleIs(reader io.Reader, buff []byte) {
  143. _, err := reader.Read(buff)
  144. if Is(err, io.EOF) {
  145. return
  146. }
  147. }
  148. func ExampleNew(UnexpectedEOF error) error {
  149. // calling New attaches the current stacktrace to the existing UnexpectedEOF error
  150. return New(UnexpectedEOF)
  151. }
  152. func ExampleWrap() error {
  153. if err := recover(); err != nil {
  154. return Wrap(err, 1)
  155. }
  156. return a()
  157. }
  158. func ExampleError_Error(err error) {
  159. fmt.Println(err.Error())
  160. }
  161. func ExampleError_ErrorStack(err error) {
  162. fmt.Println(err.(*Error).ErrorStack())
  163. }
  164. func ExampleError_Stack(err *Error) {
  165. fmt.Println(err.Stack())
  166. }
  167. func ExampleError_TypeName(err *Error) {
  168. fmt.Println(err.TypeName(), err.Error())
  169. }
  170. func ExampleError_StackFrames(err *Error) {
  171. for _, frame := range err.StackFrames() {
  172. fmt.Println(frame.File, frame.LineNumber, frame.Package, frame.Name)
  173. }
  174. }
  175. func a() error {
  176. b(5)
  177. return nil
  178. }
  179. func b(i int) {
  180. c()
  181. }
  182. func c() {
  183. panic('a')
  184. }