-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
331 lines (284 loc) · 9.52 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package errors
import (
"fmt"
_err "github.com/pkg/errors"
"strings"
)
// Fatal is a condition to see if an error can be ignored or not.
// An error value has an Fatal condition if it implements the following
// interface:
//
// type fatality interface {
// Fatal() bool
// }
//
// If the error does not implement Fatal, false will be returned.
// If the error is nil, false will be returned without further investigation.
// The logic will loop through the topmost error of the stack followed by all
// it's causes provided it implements the causer interface:
//
// type causer interface {
// Cause() error
// }
// If any one of the causes is fatal, the error is deemed fatal. i.e. irrecoverable
func Fatal(err error) (isFatal bool) {
type fatality interface {
Fatal() bool
}
// Keep going through all the errors in the stack until we hit one error which implements fatality
// We use this first error to check if the error is fatal or not.
for err != nil {
if check, ok := err.(fatality); ok {
isFatal = check.Fatal()
break
}
// Going to the cause of the current error(if any)
cause, ok := err.(causer)
if !ok {
// Since there is no cause of the current error, it is the root error(original error) that caused the issue
// in the first place. Hence breaking the loop.
break
}
err = cause.Cause()
}
return
}
// Custom error that implements:
// - cause interface from github.com/pkg/errors
// - error interface from go builtin
// - fatality interface from FSM
// It represents a rung in the chain of errors leading to the cause.
type rung struct {
msg string
cause error
fatal bool
tags map[string]string
extras map[string]interface{}
ignore bool
}
func (e *rung) Error() (errorMsg string) {
if e.msg != "" && e.cause != nil {
errorMsg = fmt.Sprintf("%v \n\t==>> %v", e.msg, e.cause)
} else if e.msg == "" && e.cause != nil {
errorMsg = fmt.Sprintf("%v", e.cause)
} else if e.msg != "" && e.cause == nil {
errorMsg = fmt.Sprintf("%s", e.msg)
}
return
}
// Implementing the causer interface from github.com/pkg/errors
func (e *rung) Cause() error {
return e.cause
}
func (e *rung) Fatal() bool {
return e.fatal
}
func (e *rung) Tags() map[string]string {
return e.tags
}
func (e *rung) Extras() map[string]interface{} {
return e.extras
}
func (e *rung) Ignore() bool {
return e.ignore
}
// NewError takes in a string input and returns an error with the input as its message
func NewError(_msg string, v ...interface{}) error {
return newError(nil, false, false, nil, nil, _msg, v...)
}
// Chain takes an error as a cause with a string message and returns an error having a cause "_cause" and it's message as "_msg". Thus it chains the new error with the input error.
func Chain(_cause error, _msg string, v ...interface{}) error {
return newError(_cause, false, false, nil, nil, _msg, v...)
}
// NewFatal returns a fatal error. Rest of it's functionality is same as NewError.
func NewFatal(_msg string, v ...interface{}) error {
return newError(nil, true, false, nil, nil, _msg, v...)
}
// ChainFatal returns a fatal error chained with the input error.
func ChainFatal(_cause error, _msg string, v ...interface{}) error {
return newError(_cause, true, false, nil, nil, _msg, v...)
}
// NewIgnorable returns an error which is ignorable by loggers.
func NewIgnorable(_msg string, v ...interface{}) error {
return newError(nil, false, true, nil, nil, _msg, v...)
}
// ChainIgnorable returns an ignorable error chained with the input error.
func ChainIgnorable(_cause error, _msg string, v ...interface{}) error {
return newError(_cause, false, true, nil, nil, _msg, v...)
}
// NewErrorWithTags returns an error with tags associated with it and fatality provided in the input
func NewErrorWithTags(_fatal bool, _tags map[string]string, _msg string, v ...interface{}) error {
return newError(nil, _fatal, false, _tags, nil, _msg, v...)
}
// ChainErrorWithTags returns an error with tags chained with the input error
func ChainErrorWithTags(_cause error, _fatal bool, _tags map[string]string, _msg string, v ...interface{}) error {
return newError(_cause, _fatal, false, _tags, nil, _msg, v...)
}
// NewErrorWithExtras returns an error with extras associated with it and fatality provided in the input
func NewErrorWithExtras(_fatal bool, _extras map[string]interface{}, _msg string, v ...interface{}) error {
return newError(nil, _fatal, false, nil, _extras, _msg, v...)
}
// ChainErrorWithExtras returns an error with extras chained with the input error
func ChainErrorWithExtras(_cause error, _fatal bool, _extras map[string]interface{}, _msg string, v ...interface{}) error {
return newError(_cause, _fatal, false, nil, _extras, _msg, v...)
}
// newError is a generic function used for creating new errors
func newError(_cause error, _fatal, ignore bool, _tags map[string]string, _extras map[string]interface{}, _msg string, v ...interface{}) error {
err := &rung{
cause: _cause,
msg: fmt.Sprintf(_msg, v...),
fatal: _fatal,
tags: _tags,
extras: _extras,
ignore: ignore,
}
return _err.WithStack(err)
}
// Based on https://godoc.org/github.com/pkg/errors#hdr-Formatted_printing_of_errors
type stackTracer interface {
StackTrace() _err.StackTrace
}
// Determines the stacktrace of an error.
// It will retrieve the entire stacktrace starting from the original root cause
func Stacktrace(err error) string {
// Printing the message of the original error
var builder strings.Builder
builder.WriteString(fmt.Sprintf("%v\n", err))
// Find the deepest element in the stack which implements the stackTracer interface
var deepestStacktracer stackTracer
for err != nil {
if val, ok := err.(stackTracer); ok {
deepestStacktracer = val
}
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
// Printing the entire stacktrace starting from the original cause of this issue
if deepestStacktracer != nil {
for _, f := range deepestStacktracer.StackTrace() {
builder.WriteString(fmt.Sprintf("%+s:%d\n", f, f))
}
}
return builder.String()
}
// Printing the stacktrace of an error.
// It will print the entire stacktrace starting from the original root cause
func PrintStackTrace(err error) {
if err != nil {
fmt.Println(Stacktrace(err))
}
}
// Copying the causer interface from pkg/errors.
// This will be used to loop over the chain of causes leading up to the topmost error
type causer interface {
Cause() error
}
// Tags returns all the tags associated with the input error and all of its causes
func Tags(err error) (cumulativeTags map[string]string) {
type tagged interface {
Tags() map[string]string
}
// Keep going through all the errors in the stack and make a cumulative map of all the tags
for err != nil {
if check, ok := err.(tagged); ok {
tagsSet := check.Tags()
if tagsSet != nil {
for k, v := range tagsSet {
if cumulativeTags == nil {
cumulativeTags = make(map[string]string)
}
// The highest error in the stack overrides the tag value set by the lower error in the stack
if _, exists := cumulativeTags[k]; !exists {
cumulativeTags[k] = v
}
}
}
}
// Going to the cause of the current error(if any)
cause, ok := err.(causer)
if !ok {
// Since there is no cause of the current error, it is the root error(original error) that caused the issue
// in the first place. Hence breaking the loop.
break
}
err = cause.Cause()
}
return
}
// Extras returns all the extras associated with the input error and all of its causes
func Extras(err error) (cumulativeExtras map[string]interface{}) {
type extra interface {
Extras() map[string]interface{}
}
// Keep going through all the errors in the stack and make a cumulative map of all the tags
for err != nil {
if check, ok := err.(extra); ok {
extrasSet := check.Extras()
if extrasSet != nil {
for k, v := range extrasSet {
if cumulativeExtras == nil {
cumulativeExtras = make(map[string]interface{})
}
// The highest error in the stack overrides the tag value set by the lower error in the stack
if _, exists := cumulativeExtras[k]; !exists {
cumulativeExtras[k] = v
}
}
}
}
// Going to the cause of the current error(if any)
cause, ok := err.(causer)
if !ok {
// Since there is no cause of the current error, it is the root error(original error) that caused the issue
// in the first place. Hence breaking the loop.
break
}
err = cause.Cause()
}
return
}
// Ignore returns true if the input error or any of its children causes are expected to be ignored. Otherwise it returns false
func Ignore(err error) bool {
type ignore interface {
Ignore() bool
}
// Keep going through all the errors in the stack and find if any error is supposed to be ignored
for err != nil {
if check, ok := err.(ignore); ok {
ignore := check.Ignore()
if ignore {
return true
}
}
// Going to the cause of the current error(if any)
cause, ok := err.(causer)
if !ok {
// Since there is no cause of the current error, it is the root error(original error) that caused the issue
// in the first place. Hence breaking the loop.
break
}
err = cause.Cause()
}
return false
}
// Finds the deepest non-nil cause
func DeepestCause(err error) error {
var cause causer
var ok bool
for err != nil {
if cause, ok = err.(causer); !ok {
// Since there is no cause of the current error, it is the root error(original error) that caused the issue
// in the first place. Hence breaking the loop.
return err
}
if cause.Cause() != nil {
err = cause.Cause()
} else {
break
}
}
return err
}