-
Notifications
You must be signed in to change notification settings - Fork 370
/
errors.go
253 lines (220 loc) · 5.68 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package errors
import (
"fmt"
"runtime"
)
// ----------------------------------------
// Convenience method.
func Wrap(cause interface{}, format string, args ...interface{}) Error {
if causeCmnError, ok := cause.(*cmnError); ok { //nolint:gocritic
msg := fmt.Sprintf(format, args...)
return causeCmnError.Stacktrace().Trace(1, msg)
} else if cause == nil {
return newCmnError(FmtError{format, args}).Stacktrace()
} else {
// NOTE: causeCmnError is a typed nil here.
msg := fmt.Sprintf(format, args...)
return newCmnError(cause).Stacktrace().Trace(1, msg)
}
}
func Cause(err error) error {
if cerr, ok := err.(*cmnError); ok {
return cerr.Data().(error)
} else {
return err
}
}
// ----------------------------------------
// Error & cmnError
/*
Usage with arbitrary error data:
```go
// Error construction
type MyError struct{}
var err1 error = NewWithData(MyError{}, "my message")
...
// Wrapping
var err2 error = Wrap(err1, "another message")
if (err1 != err2) { panic("should be the same")
...
// Error handling
switch err2.Data().(type){
case MyError: ...
default: ...
}
```
*/
type Error interface {
Error() string
Stacktrace() Error
Trace(offset int, format string, args ...interface{}) Error
Data() interface{}
}
// New Error with formatted message.
// The Error's Data will be a FmtError type.
func New(format string, args ...interface{}) Error {
err := FmtError{format, args}
return newCmnError(err)
}
// New Error with specified data.
func NewWithData(data interface{}) Error {
return newCmnError(data)
}
type cmnError struct {
data interface{} // associated data
msgtraces []msgtraceItem // all messages traced
stacktrace []uintptr // first stack trace
}
var _ Error = &cmnError{}
// NOTE: do not expose.
func newCmnError(data interface{}) *cmnError {
return &cmnError{
data: data,
msgtraces: nil,
stacktrace: nil,
}
}
// Implements error.
func (err *cmnError) Error() string {
return fmt.Sprintf("%v", err)
}
// Implements Unwrap method for compat with stdlib errors.Is()/As().
func (err *cmnError) Unwrap() error {
if err.data == nil {
return nil
}
werr, ok := err.data.(error)
if !ok {
return nil
}
return werr
}
// Captures a stacktrace if one was not already captured.
func (err *cmnError) Stacktrace() Error {
if err.stacktrace == nil {
offset := 3
depth := 32
err.stacktrace = captureStacktrace(offset, depth)
}
return err
}
// Add tracing information with msg.
// Set n=0 unless wrapped with some function, then n > 0.
func (err *cmnError) Trace(offset int, format string, args ...interface{}) Error {
msg := fmt.Sprintf(format, args...)
return err.doTrace(msg, offset)
}
// Return the "data" of this error.
// Data could be used for error handling/switching,
// or for holding general error/debug information.
func (err *cmnError) Data() interface{} {
return err.data
}
func (err *cmnError) doTrace(msg string, n int) Error {
// Ignoring linting on `runtime.Caller` for now, as it's
// a critical method that can't be currently reworked
//nolint:dogsled
pc, _, _, _ := runtime.Caller(n + 2) // +1 for doTrace(). +1 for the caller.
// Include file & line number & msg.
// Do not include the whole stack trace.
err.msgtraces = append(err.msgtraces, msgtraceItem{
pc: pc,
msg: msg,
})
return err
}
func (err *cmnError) Format(s fmt.State, verb rune) {
switch {
case verb == 'p':
s.Write([]byte(fmt.Sprintf("%p", &err)))
case verb == 'v' && s.Flag('+'):
s.Write([]byte("--= Error =--\n"))
// Write data.
fmt.Fprintf(s, "Data: %+v\n", err.data)
// Write msg trace items.
s.Write([]byte("Msg Traces:\n"))
for i, msgtrace := range err.msgtraces {
fmt.Fprintf(s, " %4d %s\n", i, msgtrace.String())
}
s.Write([]byte("--= /Error =--\n"))
case verb == 'v' && s.Flag('#'):
s.Write([]byte("--= Error =--\n"))
// Write data.
fmt.Fprintf(s, "Data: %#v\n", err.data)
// Write msg trace items.
s.Write([]byte("Msg Traces:\n"))
for i, msgtrace := range err.msgtraces {
fmt.Fprintf(s, " %4d %s\n", i, msgtrace.String())
}
// Write stack trace.
if err.stacktrace != nil {
s.Write([]byte("Stack Trace:\n"))
frames := runtime.CallersFrames(err.stacktrace)
for i := 0; ; i++ {
frame, more := frames.Next()
fmt.Fprintf(s, " %4d %s:%d\n", i, frame.File, frame.Line)
if !more {
break
}
}
}
s.Write([]byte("--= /Error =--\n"))
default:
// Write msg.
fmt.Fprintf(s, "%v", err.data)
}
}
// ----------------------------------------
// stacktrace & msgtraceItem
func captureStacktrace(offset int, depth int) []uintptr {
pcs := make([]uintptr, depth)
n := runtime.Callers(offset, pcs)
return pcs[0:n]
}
type msgtraceItem struct {
pc uintptr
msg string
}
func (mti msgtraceItem) String() string {
fnc := runtime.FuncForPC(mti.pc)
file, line := fnc.FileLine(mti.pc)
return fmt.Sprintf("%s:%d - %s",
file, line,
mti.msg,
)
}
// ----------------------------------------
// fmt error
/*
FmtError is the data type for New() (e.g. New().Data().(FmtError))
Theoretically it could be used to switch on the format string.
```go
// Error construction
var err1 error = New("invalid username %v", "BOB")
var err2 error = New("another kind of error")
...
// Error handling
switch err1.Data().(cmn.FmtError).Format() {
case "invalid username %v": ...
case "another kind of error": ...
default: ...
}
```
*/
type FmtError struct {
format string
args []interface{}
}
func (fe FmtError) Error() string {
if len(fe.args) == 0 {
return fe.format
}
return fmt.Sprintf(fe.format, fe.args...)
}
func (fe FmtError) String() string {
return fmt.Sprintf("FmtError{format:%v,args:%v}",
fe.format, fe.args)
}
func (fe FmtError) Format() string {
return fe.format
}