| 62 | } |
| 63 | |
| 64 | func ExampleCatcher_error() { |
| 65 | helper := func() error { |
| 66 | var pc Catcher |
| 67 | pc.Try(func() { panic(errors.New("error")) }) |
| 68 | return pc.Recovered().AsError() |
| 69 | } |
| 70 | |
| 71 | if err := helper(); err != nil { |
| 72 | // In normal use cases, you can use err.Error() output directly to |
| 73 | // dump the panic's stack. This is not used in the example because |
| 74 | // its output is machine-specific - instead, we demonstrate getting |
| 75 | // the underlying error that was used for the panic. |
| 76 | if cause := errors.Unwrap(err); cause != nil { |
| 77 | fmt.Printf("helper panicked with an error: %s", cause) |
| 78 | } |
| 79 | } |
| 80 | // Output: |
| 81 | // helper panicked with an error: error |
| 82 | } |
| 83 | |
| 84 | func TestCatcher(t *testing.T) { |
| 85 | t.Parallel() |