-
Notifications
You must be signed in to change notification settings - Fork 4
/
stackwrap.go
107 lines (96 loc) · 2.66 KB
/
stackwrap.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
package throw
import "reflect"
// WithStackExt wraps the error with stack of caller. Nil value will return nil.
func WithStack(err error) error {
return WithStackExt(err, 1)
}
func WithStackAndDetails(predecessor error, details interface{}) error {
return WithStackExt(WithDetails(predecessor, details), 1)
}
// WithStackTop wraps the error with stack's topmost entry only. Nil value will return nil.
// Use this method to augment an error specific to a code location.
func WithStackTop(err error) error {
return WithStackTopExt(err, 1)
}
// WithStackExt wraps the error with stack with the given number of frames skipped. Nil value will return nil.
func WithStackExt(err error, skipFrames int) error {
if err == nil {
return nil
}
if skipFrames < 0 {
skipFrames = 0
}
return withStack(err, CaptureStack(skipFrames+1))
}
// WithStackTopExt wraps the error with stack's topmost entry after skipping the given number of frames. Nil value will return nil.
func WithStackTopExt(err error, skipFrames int) error {
if err == nil {
return nil
}
if skipFrames < 0 {
skipFrames = 0
}
return withStack(err, CaptureStackTop(skipFrames+1))
}
func reuseSupersetTrace(current, wrapped StackTrace) (StackTrace, DeepestStackMode) {
switch {
case current == nil:
return wrapped, InheritedTrace
case wrapped == nil:
return nil, 0
}
switch CompareStackTraceExt(current, wrapped, SameMethod) {
case SubsetStack, StackTop:
return wrapped, InheritedTrace
case FullStack:
return nil, SupersededTrace
default:
return nil, 0
}
}
func withStack(err error, st StackTrace) stackWrap {
if sth := OutermostStack(err); sth != nil {
stDeep, _ := sth.DeepestStackTrace()
stDeepest, stDeepMod := reuseSupersetTrace(st, stDeep)
return stackWrap{st, stDeepest, stDeepMod, err}
}
return stackWrap{st: st, err: err}
}
func WithDetails(predecessor error, details ...interface{}) error {
switch {
case len(details) == 0:
return predecessor
case predecessor == nil:
return WithDetails(NewDescription(details[0]), details[1:]...)
default:
for _, d := range details {
if d == nil {
continue
}
predecessor = withDetails(predecessor, d)
}
return predecessor
}
}
func withDetails(predecessor error, details interface{}) error {
var d fmtWrap
switch vv := details.(type) {
case fmtWrap:
d = vv
case panicWrap:
d = vv.fmtWrap
case msgWrap:
d.msg = vv.msg
case stackWrap:
if vv.err == nil {
return predecessor
}
return withDetails(predecessor, vv.err)
case nil:
// nil is handled by caller
panic("illegal value")
default:
d = wrapInternal(details)
}
return detailsWrap{err: predecessor, details: d, isComparable: reflect.TypeOf(details).Comparable()}
}