forked from rs/zerolog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacktrace.go
120 lines (104 loc) · 2.59 KB
/
stacktrace.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
package pkgerrors
import (
"github.com/pkg/errors"
)
var (
StackSourceFileName = "source"
StackSourceLineName = "line"
StackSourceFunctionName = "func"
)
type state struct {
b []byte
}
// Write implement fmt.Formatter interface.
func (s *state) Write(b []byte) (n int, err error) {
s.b = b
return len(b), nil
}
// Width implement fmt.Formatter interface.
func (s *state) Width() (wid int, ok bool) {
return 0, false
}
// Precision implement fmt.Formatter interface.
func (s *state) Precision() (prec int, ok bool) {
return 0, false
}
// Flag implement fmt.Formatter interface.
func (s *state) Flag(c int) bool {
return false
}
func frameField(f errors.Frame, s *state, c rune) string {
f.Format(s, c)
return string(s.b)
}
type stackTracer interface {
StackTrace() errors.StackTrace
}
// MarshalStack implements pkg/errors stack trace marshaling.
//
// zerolog.ErrorStackMarshaler = MarshalStack
func MarshalStack(err error) interface{} {
sterr, ok := err.(stackTracer)
if !ok {
return nil
}
st := sterr.StackTrace()
s := &state{}
out := make([]map[string]string, 0, len(st))
for _, frame := range st {
out = append(out, map[string]string{
StackSourceFileName: frameField(frame, s, 's'),
StackSourceLineName: frameField(frame, s, 'd'),
StackSourceFunctionName: frameField(frame, s, 'n'),
})
}
return out
}
type stackTrace struct {
Frames []frame `json:"stacktrace"`
}
type frame struct {
StackSourceFileName string `json:"source"`
StackSourceLineName string `json:"line"`
StackSourceFuncName string `json:"func"`
}
// MarshalMultiStack properly implements pkg/errors stack trace marshaling by unwrapping the error stack.
//
// zerolog.ErrorStackMarshaler = MarshalMultiStack
func MarshalMultiStack(err error) interface{} {
stackTraces := []stackTrace{}
currentErr := err
for currentErr != nil {
stack, ok := currentErr.(stackTracer)
if !ok {
// Unwrap again because errors.Wrap actually adds two
// layers of wrapping.
currentErr = unwrapErr(currentErr)
continue
}
st := stack.StackTrace()
s := &state{}
stackTrace := stackTrace{}
for _, f := range st {
frame := frame{
StackSourceFileName: frameField(f, s, 's'),
StackSourceLineName: frameField(f, s, 'd'),
StackSourceFuncName: frameField(f, s, 'n'),
}
stackTrace.Frames = append(stackTrace.Frames, frame)
}
stackTraces = append(stackTraces, stackTrace)
currentErr = unwrapErr(currentErr)
}
return stackTraces
}
type causer interface {
Cause() error
}
func unwrapErr(err error) error {
cause, ok := err.(causer)
if !ok {
return nil
}
return cause.Cause()
}