This repository was archived by the owner on Mar 15, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +41
-6
lines changed Expand file tree Collapse file tree 2 files changed +41
-6
lines changed Original file line number Diff line number Diff line change @@ -128,22 +128,25 @@ func CatchTrace(err error) {
128
128
// digging into the root of the problem
129
129
for {
130
130
var (
131
+ ok bool
131
132
v = reflect .ValueOf (err )
132
133
fn reflect.Value
133
134
)
134
135
135
136
if v .Type ().Kind () != reflect .Struct {
136
137
break
137
- }
138
-
139
- if ! v .FieldByName ("Reason" ).IsValid () || ! v .FieldByName ("Func" ).IsValid () {
138
+ } else if ! v .FieldByName ("Reason" ).IsValid () {
140
139
break
140
+ } else if v .FieldByName ("Func" ).IsValid () {
141
+ fn = v .FieldByName ("Func" )
141
142
}
142
143
143
- fn = v .FieldByName ("Func" )
144
- err = v .FieldByName ("Reason" ).Interface ().(error )
145
-
146
144
fmt .Printf ("Place: %#v\n Reason: %s\n \n " , fn , err )
145
+
146
+ if err , ok = v .FieldByName ("Reason" ).Interface ().(error ); ! ok {
147
+ err = v .Interface ().(error )
148
+ break
149
+ }
147
150
}
148
151
149
152
panic (err )
Original file line number Diff line number Diff line change 8
8
"io/ioutil"
9
9
"log"
10
10
"os"
11
+ "strconv"
11
12
"testing"
12
13
13
14
"bou.ke/monkey"
@@ -25,6 +26,12 @@ type (
25
26
heliumErrApp struct {}
26
27
27
28
Error string
29
+
30
+ TestError struct {
31
+ Index int
32
+ Func interface {}
33
+ Reason error
34
+ }
28
35
)
29
36
30
37
const ErrTest = Error ("test" )
@@ -33,6 +40,10 @@ func (e Error) Error() string {
33
40
return string (e )
34
41
}
35
42
43
+ func (e TestError ) Error () string {
44
+ return "error level: " + strconv .Itoa (e .Index )
45
+ }
46
+
36
47
func (h heliumApp ) Run (ctx context.Context ) error { return nil }
37
48
func (h heliumErrApp ) Run (ctx context.Context ) error { return ErrTest }
38
49
@@ -253,5 +264,26 @@ func TestHelium(t *testing.T) {
253
264
254
265
require .Empty (t , exitCode )
255
266
})
267
+
268
+ t .Run ("should catch multi level errors" , func (t * testing.T ) {
269
+ var exitCode int
270
+
271
+ monkey .Patch (os .Exit , func (code int ) { exitCode = code ; panic (code ) })
272
+ monkey .Patch (log .Fatal , func (... interface {}) { exitCode = 2 ; panic (exitCode ) })
273
+
274
+ require .Panics (t , func () {
275
+ CatchTrace (TestError {
276
+ Index : 1 ,
277
+ Reason : TestError {
278
+ Index : 2 ,
279
+ Reason : TestError {
280
+ Index : 3 ,
281
+ },
282
+ },
283
+ })
284
+ })
285
+
286
+ require .Empty (t , exitCode )
287
+ })
256
288
})
257
289
}
You can’t perform that action at this time.
0 commit comments