-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
224 lines (198 loc) · 4.59 KB
/
errors.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package errors
import (
"fmt"
"errors"
"net/http"
"reflect"
"runtime"
)
type StackTraceEntry struct {
pc uintptr
file string
line int
f *runtime.Func
}
type StackTracedError interface {
error
Cause() error
CausedBy(cause error) StackTracedError
Trace() []StackTraceEntry
Complete() bool
}
type StdStackTracedError struct {
error
trace []StackTraceEntry
cause error
ellipsis bool
}
type RuntimeStackTracedError struct {
StdStackTracedError
}
func (ste *StdStackTracedError) Error() string {
return ste.error.Error()
}
func (ste *RuntimeStackTracedError) RuntimeError() {}
func (ste *StdStackTracedError) Trace() []StackTraceEntry {
return ste.trace
}
func (ste *StdStackTracedError) Cause() error {
return ste.cause
}
func (ste *StdStackTracedError) CausedBy(cause error) StackTracedError {
if ste.cause != nil {
panic("CausedBy() called twice")
}
ste.cause = cause
return ste
}
func (ste *StdStackTracedError) Complete() bool {
return !ste.ellipsis
}
var global_DEFAULT_STACK_TRACE_MAX_DEPTH int = 20
func NewStackTracedError(err error) StackTracedError {
var ste StackTracedError
var sste *StdStackTracedError
if st, isSTE := err.(*StdStackTracedError); isSTE {
return st
}
if _, isRTE := err.(runtime.Error); isRTE {
rste := &RuntimeStackTracedError{
StdStackTracedError: StdStackTracedError{error: err},
}
ste = rste
sste = &rste.StdStackTracedError
} else {
sste = &StdStackTracedError{
error: err,
}
ste = sste
}
sste.trace = make([]StackTraceEntry, 0, global_DEFAULT_STACK_TRACE_MAX_DEPTH)
for i := 1; i < global_DEFAULT_STACK_TRACE_MAX_DEPTH+2; i++ {
pc, file, line, ok := runtime.Caller(1+i)
if !ok {
break
}
if i == global_DEFAULT_STACK_TRACE_MAX_DEPTH+1 {
sste.ellipsis = true
} else {
sste.trace = append(sste.trace, StackTraceEntry{
pc: pc,
file: file,
line: line,
})
}
}
return ste
}
// Duplicated here to avoid circular package refs
type Stringable interface {
String() string
}
type WebserverError interface {
error
StatusCode() int
}
type stdWebserverError struct {
status int
msg string
}
func (we *stdWebserverError) Error() string {
return we.msg
}
func (we *stdWebserverError) StatusCode() int {
return we.status
}
func ErrBadRequest(msg string) WebserverError {
return &stdWebserverError{
status: http.StatusBadRequest,
msg: msg,
}
}
func ErrNotFound(msg string) WebserverError {
return &stdWebserverError{
status: http.StatusNotFound,
msg: msg,
}
}
func PanicToError(panicRecover interface{}) error {
if panicRecover == nil {
return nil
}
if str, isStr := panicRecover.(string); isStr {
return New(str)
} else if str, isStr := panicRecover.(Stringable); isStr {
return New(str.String())
} else if e, isErr := panicRecover.(error); isErr {
return NewStackTracedError(e)
}
return New(reflect.TypeOf(panicRecover).String())
}
func New(msg string) StackTracedError {
return NewStackTracedError(errors.New(msg))
}
func (ste *StackTraceEntry) fillIn() {
ste.f = runtime.FuncForPC(ste.pc)
}
func StackTrace(err error) string {
var buf []byte
e := err
if e != nil {
buf = append(buf, (e.Error()+"\n")...)
}
for e != nil {
if ste, ok := e.(StackTracedError); ok {
for i, entry := range ste.Trace() {
if entry.f == nil {
entry.fillIn()
}
if entry.f != nil {
file, line := entry.f.FileLine(entry.pc)
buf = append(buf, fmt.Sprintf("[%d] %s@0x%16.16X at %s:%d\n", i, entry.f.Name(), uint64(entry.pc), file, line)...)
} else {
buf = append(buf, fmt.Sprintf("[%d] ???@%16.16X at %s:%d\n", i, uint64(entry.pc), entry.file, entry.line)...)
}
}
if !ste.Complete() {
buf = append(buf,"...\n"...)
}
if ste.Cause() != nil {
buf = append(buf,fmt.Sprintf("... caused by: %s\n",ste.Cause().Error())...)
}
e = ste.Cause()
} else {
buf = append(buf,"<no stack trace available>\n"...)
e = nil
}
}
return string(buf)
}
func PrintStackTrace(err error) {
e := err
fmt.Println(e.Error())
for e != nil {
if ste, ok := e.(StackTracedError); ok {
for i, entry := range ste.Trace() {
if entry.f == nil {
entry.fillIn()
}
if entry.f != nil {
file, line := entry.f.FileLine(entry.pc)
fmt.Printf("[%d] %s@0x%16.16X at %s:%d\n", i, entry.f.Name(), uint64(entry.pc), file, line)
} else {
fmt.Printf("[%d] ???@%16.16X at %s:%d\n", i, uint64(entry.pc), entry.file, entry.line)
}
}
if !ste.Complete() {
fmt.Println("...")
}
if ste.Cause() != nil {
fmt.Println("... caused by: "+ste.Cause().Error())
}
e = ste.Cause()
} else {
fmt.Println("<no stack trace available>")
e = nil
}
}
}