-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpanic.go
More file actions
186 lines (146 loc) · 4.33 KB
/
Copy pathpanic.go
File metadata and controls
186 lines (146 loc) · 4.33 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package iopipe
import (
"encoding/json"
"fmt"
"reflect"
"runtime"
"strings"
)
const defaultErrorFrameCount = 32
const framesToPanicInfo = 3 // (top-of-stack) Callers, getPanicStack -> getPanicInfo -> beyond
func getErrorType(err interface{}) string {
errorType := reflect.TypeOf(err)
if errorType.Kind() == reflect.Ptr {
return errorType.Elem().Name()
}
return errorType.Name()
}
// InvocationError is an invocation error caught by the agent
type InvocationError struct {
Message string `json:"message"`
Name string `json:"name"`
StackTrace []*panicErrorStackFrame `json:"-"`
Stack string `json:"stack"`
}
func (h *InvocationError) Error() string {
errorJSON, _ := json.Marshal(h)
return string(errorJSON)
}
// NewPanicInvocationError returns a new panic InvocationError
func NewPanicInvocationError(err interface{}) *InvocationError {
if err == nil {
return nil
}
const framesToHide = framesToPanicInfo + 4 // here (NewPanicInvocationError) -> handler defer func -> 2 for panic -> actual error
panicInfo := getPanicInfo(err, framesToHide)
return &InvocationError{
Message: panicInfo.Message,
Name: getErrorType(err),
StackTrace: panicInfo.StackTrace,
Stack: formatStack(panicInfo.StackTrace),
}
}
// NewInvocationError returns an new InvocationError
func NewInvocationError(err error) *InvocationError {
if err == nil {
return nil
}
// Errors aren't displayed without a stack trace
frameInfo := getPanicInfo(err, 0)
return &InvocationError{
Message: getErrorMessage(err),
Name: getErrorType(err),
StackTrace: frameInfo.StackTrace,
Stack: formatStack(frameInfo.StackTrace),
}
}
type panicErrorStackFrame struct {
Path string `json:"path"`
Line int32 `json:"line"`
Function string `json:"function"`
}
type panicInfo struct {
Message string // Value passed to panic call, converted to string
StackTrace []*panicErrorStackFrame // Stack trace of the panic
}
func getPanicInfo(value interface{}, framesToHide int) panicInfo {
message := getErrorMessage(value)
stack := getPanicStack(framesToHide)
return panicInfo{Message: message, StackTrace: stack}
}
func getErrorMessage(value interface{}) string {
return fmt.Sprintf("%v", value)
}
func getPanicStack(framesToHide int) []*panicErrorStackFrame {
s := make([]uintptr, defaultErrorFrameCount)
n := runtime.Callers(framesToHide, s)
if n == 0 {
return make([]*panicErrorStackFrame, 0)
}
s = s[:n]
return convertStack(s)
}
func convertStack(s []uintptr) []*panicErrorStackFrame {
var converted []*panicErrorStackFrame
frames := runtime.CallersFrames(s)
for {
frame, more := frames.Next()
formattedFrame := formatFrame(frame)
converted = append(converted, formattedFrame)
if !more {
break
}
}
return converted
}
func formatFrame(inputFrame runtime.Frame) *panicErrorStackFrame {
path := inputFrame.File
line := int32(inputFrame.Line)
function := inputFrame.Function
// Strip GOPATH from path by counting the number of seperators in label & path
//
// For example given this:
// GOPATH = /home/user
// path = /home/user/src/pkg/sub/file.go
// label = pkg/sub.Type.Method
//
// We want to set:
// path = pkg/sub/file.go
// label = Type.Method
i := len(path)
for n, g := 0, strings.Count(function, "/")+2; n < g; n++ {
i = strings.LastIndex(path[:i], "/")
if i == -1 {
// Something went wrong and path has less seperators than we expected
// Abort and leave i as -1 to counteract the +1 below
break
}
}
path = path[i+1:] // Trim the initial /
// Strip the path from the function name as it's already in the path
function = function[strings.LastIndex(function, "/")+1:]
// Likewise strip the package name
function = function[strings.Index(function, ".")+1:]
return &panicErrorStackFrame{
Path: path,
Line: line,
Function: function,
}
}
func formatStack(inputStack []*panicErrorStackFrame) string {
s := make([]string, len(inputStack))
for i, f := range inputStack {
s[i] = fmt.Sprintf("%s:%d %s", f.Path, f.Line, f.Function)
}
return strings.Join(s, "\n")
}
func coerceInvocationError(err error) *InvocationError {
var (
ok bool
invErr *InvocationError
)
if invErr, ok = err.(*InvocationError); !ok {
invErr = NewInvocationError(err)
}
return invErr
}