example_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package errors_test
  2. import (
  3. "fmt"
  4. "github.com/pkg/errors"
  5. )
  6. func ExampleNew() {
  7. err := errors.New("whoops")
  8. fmt.Println(err)
  9. // Output: whoops
  10. }
  11. func ExampleNew_printf() {
  12. err := errors.New("whoops")
  13. fmt.Printf("%+v", err)
  14. // Example output:
  15. // whoops
  16. // github.com/pkg/errors_test.ExampleNew_printf
  17. // /home/dfc/src/github.com/pkg/errors/example_test.go:17
  18. // testing.runExample
  19. // /home/dfc/go/src/testing/example.go:114
  20. // testing.RunExamples
  21. // /home/dfc/go/src/testing/example.go:38
  22. // testing.(*M).Run
  23. // /home/dfc/go/src/testing/testing.go:744
  24. // main.main
  25. // /github.com/pkg/errors/_test/_testmain.go:106
  26. // runtime.main
  27. // /home/dfc/go/src/runtime/proc.go:183
  28. // runtime.goexit
  29. // /home/dfc/go/src/runtime/asm_amd64.s:2059
  30. }
  31. func ExampleWrap() {
  32. cause := errors.New("whoops")
  33. err := errors.Wrap(cause, "oh noes")
  34. fmt.Println(err)
  35. // Output: oh noes: whoops
  36. }
  37. func fn() error {
  38. e1 := errors.New("error")
  39. e2 := errors.Wrap(e1, "inner")
  40. e3 := errors.Wrap(e2, "middle")
  41. return errors.Wrap(e3, "outer")
  42. }
  43. func ExampleCause() {
  44. err := fn()
  45. fmt.Println(err)
  46. fmt.Println(errors.Cause(err))
  47. // Output: outer: middle: inner: error
  48. // error
  49. }
  50. func ExampleWrap_extended() {
  51. err := fn()
  52. fmt.Printf("%+v\n", err)
  53. // Example output:
  54. // error
  55. // github.com/pkg/errors_test.fn
  56. // /home/dfc/src/github.com/pkg/errors/example_test.go:47
  57. // github.com/pkg/errors_test.ExampleCause_printf
  58. // /home/dfc/src/github.com/pkg/errors/example_test.go:63
  59. // testing.runExample
  60. // /home/dfc/go/src/testing/example.go:114
  61. // testing.RunExamples
  62. // /home/dfc/go/src/testing/example.go:38
  63. // testing.(*M).Run
  64. // /home/dfc/go/src/testing/testing.go:744
  65. // main.main
  66. // /github.com/pkg/errors/_test/_testmain.go:104
  67. // runtime.main
  68. // /home/dfc/go/src/runtime/proc.go:183
  69. // runtime.goexit
  70. // /home/dfc/go/src/runtime/asm_amd64.s:2059
  71. // github.com/pkg/errors_test.fn
  72. // /home/dfc/src/github.com/pkg/errors/example_test.go:48: inner
  73. // github.com/pkg/errors_test.fn
  74. // /home/dfc/src/github.com/pkg/errors/example_test.go:49: middle
  75. // github.com/pkg/errors_test.fn
  76. // /home/dfc/src/github.com/pkg/errors/example_test.go:50: outer
  77. }
  78. func ExampleWrapf() {
  79. cause := errors.New("whoops")
  80. err := errors.Wrapf(cause, "oh noes #%d", 2)
  81. fmt.Println(err)
  82. // Output: oh noes #2: whoops
  83. }
  84. func ExampleErrorf_extended() {
  85. err := errors.Errorf("whoops: %s", "foo")
  86. fmt.Printf("%+v", err)
  87. // Example output:
  88. // whoops: foo
  89. // github.com/pkg/errors_test.ExampleErrorf
  90. // /home/dfc/src/github.com/pkg/errors/example_test.go:101
  91. // testing.runExample
  92. // /home/dfc/go/src/testing/example.go:114
  93. // testing.RunExamples
  94. // /home/dfc/go/src/testing/example.go:38
  95. // testing.(*M).Run
  96. // /home/dfc/go/src/testing/testing.go:744
  97. // main.main
  98. // /github.com/pkg/errors/_test/_testmain.go:102
  99. // runtime.main
  100. // /home/dfc/go/src/runtime/proc.go:183
  101. // runtime.goexit
  102. // /home/dfc/go/src/runtime/asm_amd64.s:2059
  103. }
  104. func Example_stacktrace() {
  105. type stacktracer interface {
  106. StackTrace() errors.StackTrace
  107. }
  108. err, ok := errors.Cause(fn()).(stacktracer)
  109. if !ok {
  110. panic("oops, err does not implement stacktracer")
  111. }
  112. st := err.StackTrace()
  113. fmt.Printf("%+v", st[0:2]) // top two frames
  114. // Example output:
  115. // github.com/pkg/errors_test.fn
  116. // /home/dfc/src/github.com/pkg/errors/example_test.go:47
  117. // github.com/pkg/errors_test.Example_stacktrace
  118. // /home/dfc/src/github.com/pkg/errors/example_test.go:127
  119. }
  120. func ExampleCause_printf() {
  121. err := errors.Wrap(func() error {
  122. return func() error {
  123. return errors.Errorf("hello %s", fmt.Sprintf("world"))
  124. }()
  125. }(), "failed")
  126. fmt.Printf("%v", err)
  127. // Output: failed: hello world
  128. }