-
Notifications
You must be signed in to change notification settings - Fork 0
/
catched_error.go
508 lines (420 loc) · 11.4 KB
/
catched_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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
package errors
import (
"errors"
"fmt"
"sync/atomic"
)
// WrappedError is a structure defining wrapped error. It's public to be able
// customize logging and failed HTTP responses.
type WrappedError struct {
// Message holds final error's Message.
Message string `json:"message"`
// Severity) holds severity level.
Severity SeverityLevel `json:"severity"`
// StatusCode holds HTTP status code, what is recommended to assign to HTTP response
// if value is specified (above zero).
StatusCode int `json:"-"`
// Code holds application error code.
Code string `json:"code,omitempty"`
// Protected
Protected bool `json:"-"`
// err holds initial error.
err error
isMessageReplaced bool
}
// Error implements standard Error interface.
func (we WrappedError) Error() string {
return we.Message
}
// Err returns original error.
func (we WrappedError) Err() error {
if we.err != nil {
return we.err
}
return errors.New(we.Message)
}
// CatchedError holds original error, all intermediate error wraps and the call stack.
type CatchedError struct {
frames []Frame
fields map[string]interface{}
lasterr WrappedError
werrs []WrappedError
isRecatched bool
isStackAdded bool
uid uint64
}
var uid uint64
// CatchedError implements golang standard Error interface. Returns string, taking into
// account setting ErrorMethodMode.
//
// If ErrorMethodMode == Multi, results build using LIFO principle.
func (ce CatchedError) Error() string {
res := ce.lasterr.Message
if ErrorMethodMode == Single {
return res
}
// Multi
for i := len(ce.werrs) - 1; i >= 0; i-- {
if msg := ce.werrs[i].Message; msg != "" {
res += ": " + msg
}
}
return res
}
// Frames returns callstack frames.
func (ce *CatchedError) Frames() []Frame {
return ce.frames
}
// Fields returns all key/value pairs associated with error.
func (ce *CatchedError) Fields() map[string]interface{} {
return ce.fields
}
// Last returns the last wrap of error .
func (ce *CatchedError) Last() WrappedError {
return ce.lasterr
}
// Len returns amount of errors wrapped + 1 or zero if nil.
func (ce *CatchedError) Len() int {
if ce == nil {
return 0
}
return 1 + len(ce.werrs)
}
// WrappedErrors returns all errors holding by CatchedError.
func (ce *CatchedError) WrappedErrors() []WrappedError {
res := make([]WrappedError, 1+len(ce.werrs))
res[0] = ce.lasterr
if len(ce.werrs) > 0 {
copy(res[1:], ce.werrs)
}
return res
}
// Alarmer wraps a single method Alarm that receives CatchedError and implement real-time
// notification logic.
//
// The type CapturedError has method Alarm that recieves Alarmer as a parameter.
type Alarmer interface {
Alarm(*CatchedError)
}
// New returns *CatchedError with stack at the point of calling and severity level Tiny.
// The function is used if there is no original error what can be wrapped.
func New(msg string) *CatchedError {
return newx(msg, true)
}
// NewTiny is synonym to func New.
func NewTiny(msg string) *CatchedError {
return newx(msg, true)
}
// NewMedium returns *CatchedError with stack at the point of calling and severity level Medium.
func NewMedium(msg string) *CatchedError {
return newx(msg, true).Severity(Medium)
}
// NewCritical returns CatchedError with stack at the point of calling and severity level Critical.
func NewCritical(msg string) *CatchedError {
return newx(msg, true).Severity(Critical)
}
func newx(msg string, stack bool) *CatchedError {
res := CatchedError{lasterr: WrappedError{Message: msg, isMessageReplaced: false}, uid: atomic.AddUint64(&uid, 1)}
if stack {
res.frames = CallerFramesFunc(1)
}
return &res
}
// Catch wraps an error with capturing stack at the point of calling.
// Assigns severity level Tiny.
//
// If err is already CapturedError, the stack does not capture again. It's assumed
// it was done before. The attributes Severity and StatusCode inherits but can be changed later.
//
// Returns nil if err is nil.
func Catch(err error) *CatchedError {
if err == nil {
return nil
}
return catch(err, 0)
}
func Wrap(err error, ce *CatchedError) *CatchedError {
if err == nil {
return nil
}
e := catch(err, 0)
if len(ce.fields) > 0 {
if len(e.fields) == 0 {
e.fields = ce.fields
} else {
for k, v := range ce.fields {
e.fields[k] = v
}
}
}
e.werrs = append(e.werrs, e.lasterr)
e.lasterr = ce.lasterr
e.uid = ce.uid
return e
}
// Raise returns explicitly defined CatchedError. Function captures stack at the point of calling.
func Raise(ce *CatchedError) *CatchedError {
return ce.Raise()
}
// CatchCustom wraps an error with custom stack capturer.
func CatchCustom(err error, stackcapture func() []Frame) *CatchedError {
if err == nil {
return nil
}
return &CatchedError{
lasterr: WrappedError{Message: err.Error(), err: err, isMessageReplaced: false},
frames: stackcapture(),
isStackAdded: true}
}
func catch(err error, callerOffset int) *CatchedError {
if ce, ok := err.(*CatchedError); ok && ce != nil {
// message still stay the same! It's expected message will be replaced later by calling Msg().
(*ce).isRecatched = true
return ce
}
return &CatchedError{
lasterr: WrappedError{
Message: err.Error(),
err: err,
},
frames: CallerFramesFunc(callerOffset),
isStackAdded: true,
uid: atomic.AddUint64(&uid, 1),
}
}
// Capture captures stack frames. Recommended to use when raised predefined errors.
//
// var ErrInvalidCustomerID = errors.New("invalid customer id")
//
// if c, ok := customers[id]; ok {
// return ErrInvalidCustomerID.Capture()
// }
func (ce *CatchedError) Capture() *CatchedError {
// ce.frames = CallerFramesFunc(0)
// ce.isStackAdded = true
// return ce
return ce.Raise()
}
// Capture captures stack frames. Recommended to use when raised predefined errors.
//
// var ErrInvalidCustomerID = errors.New("invalid customer id")
//
// if c, ok := customers[id]; ok {
// return ErrInvalidCustomerID.Capture()
// }
func (ce *CatchedError) Raise() *CatchedError {
res := CatchedError{
frames: CallerFramesFunc(0),
lasterr: ce.lasterr,
isRecatched: false,
isStackAdded: true,
uid: ce.uid,
}
if len(ce.fields) > 0 {
res.fields = make(map[string]interface{}, len(ce.fields))
for k, v := range ce.fields {
res.fields[k] = v
}
}
if len(ce.werrs) > 0 {
res.werrs = append(res.werrs, ce.werrs...)
}
return &res
}
// Msg sets or replaces latest error's text message.
// If message different previous error pushed to error stack.
func (ce *CatchedError) Msg(s string) error {
return ce.msg(s)
}
func (ce *CatchedError) msg(s string) *CatchedError {
if ce == nil {
return nil
}
if ce.lasterr.Message == s {
return ce
}
ce.werrs = append(ce.werrs, ce.lasterr)
ce.lasterr.Message = s
return ce
}
// WrappedMessages returns error messages of wrapped errors except last message.
func (ce *CatchedError) WrappedMessages(exceptProtected bool) []string {
return ce.messages(false, exceptProtected)
}
// AllMessages returns all error text messages including top (last) message.
// The last message is in the beginning of slice.
func (ce *CatchedError) AllMessages(exceptProtected bool) []string {
return ce.messages(true, exceptProtected)
}
func (ce *CatchedError) messages(includeTop, exceptProtected bool) []string {
var res []string
if includeTop {
if !(exceptProtected && ce.lasterr.Protected) && ce.lasterr.Message != "" {
res = append(res, ce.lasterr.Message)
}
}
for i := len(ce.werrs) - 1; i >= 0; i-- {
if ce.werrs[i].Protected && exceptProtected {
continue
}
res = append(res, ce.werrs[i].Message)
}
return res
}
// Severity overwrites error's severity level.
func (ce *CatchedError) Severity(level SeverityLevel) *CatchedError {
if ce == nil {
return nil
}
ce.lasterr.Severity = level
return ce
}
func (ce *CatchedError) GetSeverity() SeverityLevel {
return ce.lasterr.Severity
}
func (ce *CatchedError) GetCode() string {
return ce.lasterr.Code
}
// Medium sets severity level to Medium. It's ignored if current level Critical.
func (ce *CatchedError) Medium() *CatchedError {
return ce.Severity(Medium)
}
// Critical sets severity level to Critical.
func (ce *CatchedError) Critical() *CatchedError {
return ce.Severity(Critical)
}
// StatusCode sets HTTP response code, recommended to be assigned.
// StatusCode 404 is used by
func (ce *CatchedError) StatusCode(code int) *CatchedError {
if ce == nil {
return nil
}
ce.lasterr.StatusCode = code
return ce
}
// Code sets business code of.
func (ce *CatchedError) Code(code string) *CatchedError {
if ce == nil {
return nil
}
ce.lasterr.Code = code
return ce
}
// Protect marks error as protected. An error with protection will not be
// visible to the user.
func (ce *CatchedError) Protect() *CatchedError {
if ce == nil {
return nil
}
ce.lasterr.Protected = true
return ce
}
// Set associates a single key with value.
func (ce *CatchedError) Set(key string, val interface{}) *CatchedError {
if ce == nil {
return nil
}
if ce.fields == nil {
ce.fields = map[string]interface{}{}
}
ce.fields[key] = val
return ce
}
// LastNonPairedValue holds value to be assigned by SetPairs if amount of parameters is odd.
var LastNonPairedValue interface{} = "missed value"
// SetPairs associates multiple key/value pairs. SetPairs("id", 10, "name", "John")
// if amount of parameters is odd, SetPairs("id", 10, "name") uses LastNonPairedValue
// as the last value.
func (ce *CatchedError) SetPairs(kvpairs ...interface{}) *CatchedError {
if ce == nil {
return nil
}
if len(kvpairs) == 0 {
return ce
}
even := true
if len(kvpairs)%2 != 0 {
even = false
}
if ce.fields == nil {
ce.fields = map[string]interface{}{}
}
for i := 0; i < len(kvpairs); i += 2 {
key := fmt.Sprintf("%s", kvpairs[i])
var val interface{}
if !even && i == len(kvpairs)-1 {
val = LastNonPairedValue
} else {
val = kvpairs[i+1]
}
ce.fields[key] = val
}
return ce
}
// SetVals accociates with the key multiple interfaces.
func (ce *CatchedError) SetVals(key string, vals ...interface{}) *CatchedError {
if ce == nil {
return nil
}
if ce.fields == nil {
ce.fields = map[string]interface{}{}
}
ce.fields[key] = vals
return ce
}
// SetStrs accociates with the key multiple strings.
func (ce *CatchedError) SetStrs(key string, strs ...string) *CatchedError {
if ce == nil {
return nil
}
if ce.fields == nil {
ce.fields = map[string]interface{}{}
}
ce.fields[key] = strs
return ce
}
// Get returns value by key.
func (ce *CatchedError) Get(key string) (interface{}, bool) {
if ce == nil {
return nil, false
}
if ce.fields == nil {
return nil, false
}
res, ok := ce.fields[key]
return res, ok
}
// GetDefault returns value by key. Returns def if not found.
func (ce *CatchedError) GetDefault(key string, def interface{}) interface{} {
if ce == nil {
return def
}
if ce.fields == nil {
return def
}
res, ok := ce.fields[key]
if !ok {
return def
}
return res
}
// IsNotFound returns true if StatusCode is 404.
func (ce *CatchedError) IsNotFound() bool {
return IsNotFound(ce)
}
// IsNotFound returns true if err is *CatchedError and StatusCode is 404.
// If err wraps another errors, last one is taken for decision.
func IsNotFound(err error) bool {
if err == nil {
return false
}
if ce, ok := err.(*CatchedError); ok {
return ce.lasterr.StatusCode == 404
}
return false
}
// Alarm send error to Alarmer.
// Intended usage is real-time SRE notification if critical error.
func (ce *CatchedError) Alarm(a Alarmer) {
a.Alarm(ce)
}