error_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package errors
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "runtime/debug"
  7. "strings"
  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. 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. if !strings.HasSuffix(original.StackFrames()[0].File, "error_test.go") || strings.HasSuffix(original.StackFrames()[1].File, "error_test.go") {
  120. t.Errorf("Skip failed")
  121. }
  122. }
  123. func ExampleErrorf(x int) (int, error) {
  124. if x%2 == 1 {
  125. return 0, Errorf("can only halve even numbers, got %d", x)
  126. }
  127. return x / 2, nil
  128. }
  129. func ExampleWrapError() (error, error) {
  130. // Wrap io.EOF with the current stack-trace and return it
  131. return nil, Wrap(io.EOF, 0)
  132. }
  133. func ExampleWrapError_skip() {
  134. defer func() {
  135. if err := recover(); err != nil {
  136. // skip 1 frame (the deferred function) and then return the wrapped err
  137. err = Wrap(err, 1)
  138. }
  139. }()
  140. }
  141. func ExampleIs(reader io.Reader, buff []byte) {
  142. _, err := reader.Read(buff)
  143. if Is(err, io.EOF) {
  144. return
  145. }
  146. }
  147. func ExampleNew(UnexpectedEOF error) error {
  148. // calling New attaches the current stacktrace to the existing UnexpectedEOF error
  149. return New(UnexpectedEOF)
  150. }
  151. func ExampleWrap() error {
  152. if err := recover(); err != nil {
  153. return Wrap(err, 1)
  154. }
  155. return a()
  156. }
  157. func ExampleError_Error(err error) {
  158. fmt.Println(err.Error())
  159. }
  160. func ExampleError_ErrorStack(err error) {
  161. fmt.Println(err.(*Error).ErrorStack())
  162. }
  163. func ExampleError_Stack(err *Error) {
  164. fmt.Println(err.Stack())
  165. }
  166. func ExampleError_TypeName(err *Error) {
  167. fmt.Println(err.TypeName(), err.Error())
  168. }
  169. func ExampleError_StackFrames(err *Error) {
  170. for _, frame := range err.StackFrames() {
  171. fmt.Println(frame.File, frame.LineNumber, frame.Package, frame.Name)
  172. }
  173. }
  174. func a() error {
  175. b(5)
  176. return nil
  177. }
  178. func b(i int) {
  179. c()
  180. }
  181. func c() {
  182. panic('a')
  183. }