-
Notifications
You must be signed in to change notification settings - Fork 0
/
cause.go
62 lines (52 loc) 路 751 Bytes
/
cause.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
package terrors
import (
"errors"
)
type Framer interface {
error
Root() error
Frame() Frame
Detail() string
Simple() string
}
func Cause2(err error) (f Framer, r bool) {
for {
we, ok := err.(Framer)
if !ok {
return
}
r = r || ok
f = we
err = we.Root()
if err == nil {
return
}
}
}
func ListCause(err error) ([]Framer, bool) {
var frames []Framer
for {
we, ok := err.(Framer)
if !ok {
return frames, ok
}
frames = append(frames, we)
err = we.Root()
if err == nil {
return frames, ok
}
}
}
func FirstCause(err error) (Framer, bool) {
for {
if err == nil {
return nil, false
}
frm, ok := err.(Framer)
if !ok {
err = errors.Unwrap(err)
} else {
return frm, true
}
}
}