-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacktrace.go
116 lines (100 loc) · 2.33 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
package goerr
import (
"encoding/json"
"strings"
)
// StackTrace is an object that represents a stack trace for a given error,
// create new instances with NewStackTrace.
type StackTrace struct {
Error error
Cause error
ErrorMsg string
ErrorCtx map[string]interface{}
Stack []*StackFrame
}
// NewStackTrace is the constructor for StackTrace
func NewStackTrace(err error) *StackTrace {
st := &StackTrace{
Error: err,
Cause: Cause(err),
ErrorMsg: err.Error(),
}
// Assign any additional context values
st.ErrorCtx = marshalError(st.Cause)
// Grab all the frames from each error in the error chain
frames := []*StackFrame{}
var e *Error
if As(err, &e) {
for {
frames = append(frames, e.Frame())
unWrapped := Unwrap(e)
if unWrapped == nil {
break
}
unWrappedCasted, ok := unWrapped.(*Error)
if !ok {
break
}
if unWrappedCasted.caller == 0 {
break
}
e = unWrappedCasted
}
}
if len(frames) > 0 {
// Reverse the frames so we create a call stack in the expected manner.
// ie: from the error cause to the root of the program
// https://github.com/golang/go/wiki/SliceTricks#reversing
for i := len(frames)/2 - 1; i >= 0; i-- {
opp := len(frames) - 1 - i
frames[i], frames[opp] = frames[opp], frames[i]
}
st.Stack = frames
}
return st
}
// String implements the Stringer interface
func (s *StackTrace) String() string {
st := s.ErrorMsg + "\n\n"
if s.ErrorCtx != nil {
ctx, err := json.MarshalIndent(s.ErrorCtx, "", " ")
if err != nil {
panic(err)
}
st = st + string(ctx) + "\n\n"
}
if s.Stack != nil {
for _, f := range s.Stack {
st = st + f.String()
}
st = st + "\n"
}
return st
}
// MarshalJSON implements the Marshaler interface
// see https://golang.org/pkg/encoding/json/#Marshaler
func (s *StackTrace) MarshalJSON() ([]byte, error) {
data := map[string]interface{}{
"error-msg": s.ErrorMsg,
}
if s.ErrorCtx != nil {
data["error-ctx"] = s.ErrorCtx
}
if s.Stack != nil {
data["stack"] = s.Stack
}
return json.Marshal(data)
}
func marshalError(err error) map[string]interface{} {
if j, jerr := json.Marshal(err); jerr == nil {
jS := string(j)
if strings.HasPrefix(jS, "{") && jS != "{}" {
var out map[string]interface{}
if err := json.Unmarshal(j, &out); err != nil {
return nil
}
return out
}
}
return nil
}