forked from timandy/routine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
174 lines (158 loc) · 4.3 KB
/
error.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
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
package routine
import (
"bytes"
"fmt"
"reflect"
"runtime"
"strconv"
"unicode"
)
const (
newLine = "\n"
innerErrorPrefix = " ---> "
endOfInnerErrorStack = "--- End of inner error stack trace ---"
endOfErrorStack = "--- End of error stack trace ---"
wordAt = "at"
wordIn = "in"
wordCreatedBy = "created by"
)
type runtimeError struct {
goid int64
gopc uintptr
message string
stackTrace []uintptr
cause RuntimeError
}
func (re *runtimeError) Goid() int64 {
return re.goid
}
func (re *runtimeError) Gopc() uintptr {
return re.gopc
}
func (re *runtimeError) Message() string {
return re.message
}
func (re *runtimeError) StackTrace() []uintptr {
return re.stackTrace
}
func (re *runtimeError) Cause() RuntimeError {
return re.cause
}
func (re *runtimeError) Error() string {
return runtimeErrorError(re)
}
func runtimeErrorNew(cause any) (goid int64, gopc uintptr, msg string, stackTrace []uintptr, innerErr RuntimeError) {
runtimeErr, isRuntimeErr := cause.(RuntimeError)
if !isRuntimeErr {
if err, isErr := cause.(error); isErr {
msg = err.Error()
} else if cause != nil {
msg = fmt.Sprint(cause)
}
}
gp := getg()
return gp.goid, *gp.gopc, msg, captureStackTrace(2, 100), runtimeErr
}
func runtimeErrorNewWithMessage(message string) (goid int64, gopc uintptr, msg string, stackTrace []uintptr, innerErr RuntimeError) {
gp := getg()
return gp.goid, *gp.gopc, message, captureStackTrace(2, 100), nil
}
func runtimeErrorNewWithMessageCause(message string, cause any) (goid int64, gopc uintptr, msg string, stackTrace []uintptr, innerErr RuntimeError) {
runtimeErr, isRuntimeErr := cause.(RuntimeError)
if !isRuntimeErr {
causeMsg := ""
if err, isErr := cause.(error); isErr {
causeMsg = err.Error()
} else if cause != nil {
causeMsg = fmt.Sprint(cause)
}
if len(message) == 0 {
message = causeMsg
} else if len(causeMsg) != 0 {
message += " - " + causeMsg
}
}
gp := getg()
return gp.goid, *gp.gopc, message, captureStackTrace(2, 100), runtimeErr
}
func runtimeErrorError(re RuntimeError) string {
builder := &bytes.Buffer{}
runtimeErrorPrintStackTrace(re, builder)
runtimeErrorPrintCreatedBy(re, builder)
return builder.String()
}
func runtimeErrorPrintStackTrace(re RuntimeError, builder *bytes.Buffer) {
builder.WriteString(runtimeErrorTypeName(re))
message := re.Message()
if len(message) > 0 {
builder.WriteString(": ")
builder.WriteString(message)
}
cause := re.Cause()
if cause != nil {
builder.WriteString(newLine)
builder.WriteString(innerErrorPrefix)
runtimeErrorPrintStackTrace(cause, builder)
builder.WriteString(newLine)
builder.WriteString(" ")
builder.WriteString(endOfInnerErrorStack)
}
stackTrace := re.StackTrace()
if stackTrace != nil {
savePoint := builder.Len()
skippedPanic := false
frames := runtime.CallersFrames(stackTrace)
for {
frame, more := frames.Next()
if showFrame(frame.Function) {
builder.WriteString(newLine)
builder.WriteString(" ")
builder.WriteString(wordAt)
builder.WriteString(" ")
builder.WriteString(frame.Function)
builder.WriteString("() ")
builder.WriteString(wordIn)
builder.WriteString(" ")
builder.WriteString(frame.File)
builder.WriteString(":")
builder.WriteString(strconv.Itoa(frame.Line))
} else if skipFrame(frame.Function, skippedPanic) {
builder.Truncate(savePoint)
skippedPanic = true
}
if !more {
break
}
}
}
}
func runtimeErrorPrintCreatedBy(re RuntimeError, builder *bytes.Buffer) {
goid := re.Goid()
if goid == 1 {
return
}
pc := re.Gopc()
frame, _ := runtime.CallersFrames([]uintptr{pc}).Next()
if frame.Func == nil {
return
}
builder.WriteString(newLine)
builder.WriteString(" ")
builder.WriteString(endOfErrorStack)
builder.WriteString(newLine)
builder.WriteString(" ")
builder.WriteString(wordCreatedBy)
builder.WriteString(" ")
builder.WriteString(frame.Function)
builder.WriteString("() ")
builder.WriteString(wordIn)
builder.WriteString(" ")
builder.WriteString(frame.File)
builder.WriteString(":")
builder.WriteString(strconv.Itoa(frame.Line))
}
func runtimeErrorTypeName(re RuntimeError) string {
typeName := []rune(reflect.TypeOf(re).Elem().Name())
typeName[0] = unicode.ToUpper(typeName[0])
return string(typeName)
}