-
Notifications
You must be signed in to change notification settings - Fork 30
/
panic_test.go
179 lines (139 loc) · 3.53 KB
/
panic_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package errorx
import (
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestPanic(t *testing.T) {
defer func() {
r := recover()
require.NotNil(t, r)
output := fmt.Sprintf("%v", r)
require.Contains(t, output, "errorx.funcWithErr()", output)
}()
Panic(funcWithErr())
}
func TestPanicErrorx(t *testing.T) {
defer func() {
r := recover()
require.NotNil(t, r)
output := fmt.Sprintf("%v", r)
require.Contains(t, output, "awful", output)
require.Contains(t, output, "errorx.funcWithBadPanic()", output)
}()
funcWithBadPanic()
}
func TestPanicRecover(t *testing.T) {
defer func() {
r := recover()
require.NotNil(t, r)
err, ok := ErrorFromPanic(r)
require.True(t, ok)
output := fmt.Sprintf("%+v", err)
require.Contains(t, output, "errorx.funcWithErr()", output)
require.Contains(t, output, "bad", output)
require.True(t, IsOfType(err, testType))
}()
Panic(funcWithErr())
}
func TestPanicRecoverNoTrace(t *testing.T) {
defer func() {
r := recover()
require.NotNil(t, r)
err, ok := ErrorFromPanic(r)
require.True(t, ok)
output := fmt.Sprintf("%+v", err)
require.NotContains(t, output, "errorx.funcWithErrNoTrace()", output)
require.Contains(t, output, "errorx.funcWithPanicNoTrace()", output)
require.Contains(t, output, "silent", output)
require.True(t, IsOfType(err, testType))
}()
funcWithPanicNoTrace()
}
func TestPanicRecoverNoErrorx(t *testing.T) {
defer func() {
r := recover()
require.NotNil(t, r)
err, ok := ErrorFromPanic(r)
require.True(t, ok)
output := fmt.Sprintf("%+v", err)
require.NotContains(t, output, "errorx.funcWithBadErr()", output)
require.Contains(t, output, "errorx.funcWithBadPanic()", output)
require.Contains(t, output, "awful", output)
require.False(t, IsOfType(err, testType))
}()
funcWithBadPanic()
}
func funcWithErr() error {
return testType.New("bad")
}
func funcWithPanicNoTrace() {
Panic(funcWithErrNoTrace())
}
func funcWithErrNoTrace() error {
return testTypeSilent.New("silent")
}
func funcWithBadPanic() {
Panic(funcWithBadErr())
}
func funcWithBadErr() error {
return errors.New("awful")
}
func TestPanicChain(t *testing.T) {
ch0 := make(chan error, 1)
ch1 := make(chan error, 1)
go doMischief(ch1)
go doMoreMischief(ch0, ch1)
select {
case err := <-ch0:
require.Error(t, err)
require.False(t, IsOfType(err, AssertionFailed))
output := fmt.Sprintf("%+v", err)
require.Contains(t, output, "mischiefProper", output)
require.Contains(t, output, "mischiefAsPanic", output)
require.Contains(t, output, "doMischief", output)
require.Contains(t, output, "handleMischief", output)
require.NotContains(t, output, "doMoreMischief", output) // stack trace is only enhanced in Panic, not in user code
t.Log(output)
case <-time.After(time.Second):
require.Fail(t, "expected error")
}
}
func doMoreMischief(ch0 chan error, ch1 chan error) {
defer func() {
if e := recover(); e != nil {
err, ok := ErrorFromPanic(e)
if ok {
ch0 <- Decorate(err, "hop 2")
return
}
}
ch0 <- AssertionFailed.New("test failed")
}()
handleMischief(ch1)
}
func handleMischief(ch chan error) {
err := <-ch
Panic(Decorate(err, "handle"))
}
func doMischief(ch chan error) {
defer func() {
if e := recover(); e != nil {
err, ok := ErrorFromPanic(e)
if ok {
ch <- Decorate(err, "hop 1")
return
}
}
ch <- AssertionFailed.New("test failed")
}()
mischiefAsPanic()
}
func mischiefAsPanic() {
Panic(mischiefProper())
}
func mischiefProper() error {
return ExternalError.New("mischief")
}