error_test.go 4.5 KB

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