-
Notifications
You must be signed in to change notification settings - Fork 22
/
errors_multi.go
477 lines (370 loc) · 9.83 KB
/
errors_multi.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
/*
Error Catcher
The MutiCatcher type makes it possible to collect from a group of
operations and then aggregate them as a single error.
*/
package grip
import (
"fmt"
"strings"
"sync"
"github.com/pkg/errors"
)
// TODO: make a new catcher package, leave constructors in this
// package, use this Catcher interface, and write implementations that
// don't translate errors into string internally.
//
// CheckFunction are functions which take no arguments and return an
// error.
type CheckFunction func() error
// Catcher is an interface for an error collector for use when
// implementing continue-on-error semantics in concurrent
// operations. There are three different Catcher implementations
// provided by this package that differ *only* in terms of the
// string format returned by String() (and also the format of the
// error returned by Resolve().)
//
// If you do not use github.com/pkg/errors to attach
// errors, the implementations are usually functionally
// equivalent. The Extended variant formats the errors using the "%+v"
// (which returns a full stack trace with pkg/errors,) the Simple
// variant uses %s (which includes all the wrapped context,) and the
// basic catcher calls error.Error() (which should be equvalent to %s
// for most error implementations.)
type Catcher interface {
Add(error)
AddWhen(bool, error)
Extend([]error)
ExtendWhen(bool, []error)
Len() int
HasErrors() bool
String() string
Resolve() error
Errors() []error
New(string)
NewWhen(bool, string)
Errorf(string, ...interface{})
ErrorfWhen(bool, string, ...interface{})
Wrap(error, string)
Wrapf(error, string, ...interface{})
Check(CheckFunction)
CheckExtend([]CheckFunction)
CheckWhen(bool, CheckFunction)
}
// multiCatcher provides an interface to collect and coalesse error
// messages within a function or other sequence of operations. Used to
// implement a kind of "continue on error"-style operations. The
// methods on MultiCatatcher are thread-safe.
type baseCatcher struct {
errs []error
mutex sync.RWMutex
fmt.Stringer
}
// NewCatcher returns a Catcher instance that you can use to capture
// error messages and aggregate the errors. For consistency with
// earlier versions NewCatcher is the same as NewExtendedCatcher()
//
// DEPRECATED: use one of the other catcher implementations. See the
// documentation for the Catcher interface for most implementations.
func NewCatcher() Catcher { return NewExtendedCatcher() }
// NewBasicCatcher collects error messages and formats them using a
// new-line separated string of the output of error.Error()
func NewBasicCatcher() Catcher {
c := &baseCatcher{}
c.Stringer = &basicCatcher{c}
return c
}
// NewSimpleCatcher collects error messages and formats them using a
// new-line separated string of the string format of the error message
// (e.g. %s).
func NewSimpleCatcher() Catcher {
c := &baseCatcher{}
c.Stringer = &simpleCatcher{c}
return c
}
// NewExtendedCatcher collects error messages and formats them using a
// new-line separated string of the extended string format of the
// error message (e.g. %+v).
func NewExtendedCatcher() Catcher {
c := &baseCatcher{}
c.Stringer = &extendedCatcher{c}
return c
}
// Add takes an error object and, if it's non-nil, adds it to the
// internal collection of errors.
func (c *baseCatcher) Add(err error) {
if err == nil {
return
}
c.mutex.Lock()
defer c.mutex.Unlock()
c.errs = append(c.errs, err)
}
// Len returns the number of errors stored in the collector.
func (c *baseCatcher) Len() int {
c.mutex.RLock()
defer c.mutex.RUnlock()
return len(c.errs)
}
// HasErrors returns true if the collector has ingested errors, and
// false otherwise.
func (c *baseCatcher) HasErrors() bool {
c.mutex.RLock()
defer c.mutex.RUnlock()
return len(c.errs) > 0
}
// Extend adds all non-nil errors, passed as arguments to the catcher.
func (c *baseCatcher) Extend(errs []error) {
if len(errs) == 0 {
return
}
c.mutex.Lock()
defer c.mutex.Unlock()
for _, err := range errs {
if err == nil {
continue
}
c.errs = append(c.errs, err)
}
}
func (c *baseCatcher) Errorf(form string, args ...interface{}) {
if form == "" {
return
} else if len(args) == 0 {
c.New(form)
return
}
c.Add(errors.Errorf(form, args...))
}
func (c *baseCatcher) New(e string) {
if e == "" {
return
}
c.Add(errors.New(e))
}
func (c *baseCatcher) Wrap(err error, m string) { c.Add(errors.Wrap(err, m)) }
func (c *baseCatcher) Wrapf(err error, f string, args ...interface{}) {
c.Add(errors.Wrapf(err, f, args...))
}
func (c *baseCatcher) AddWhen(cond bool, err error) {
if !cond {
return
}
c.Add(err)
}
func (c *baseCatcher) ExtendWhen(cond bool, errs []error) {
if !cond {
return
}
c.Extend(errs)
}
func (c *baseCatcher) ErrorfWhen(cond bool, form string, args ...interface{}) {
if !cond {
return
}
c.Errorf(form, args...)
}
func (c *baseCatcher) NewWhen(cond bool, e string) {
if !cond {
return
}
c.New(e)
}
func (c *baseCatcher) Check(fn CheckFunction) { c.Add(fn()) }
func (c *baseCatcher) CheckWhen(cond bool, fn CheckFunction) {
if !cond {
return
}
c.Add(fn())
}
func (c *baseCatcher) CheckExtend(fns []CheckFunction) {
for _, fn := range fns {
c.Add(fn())
}
}
func (c *baseCatcher) Errors() []error {
c.mutex.RLock()
defer c.mutex.RUnlock()
out := make([]error, len(c.errs))
copy(out, c.errs)
return out
}
// Resolve returns a final error object for the Catcher. If there are
// no errors, it returns nil, and returns an error object with the
// string form of all error objects in the collector.
func (c *baseCatcher) Resolve() error {
if !c.HasErrors() {
return nil
}
return errors.New(c.String())
}
////////////////////////////////////////////////////////////////////////
//
// separate implementations of grip.Catcher with different string formatting options.
type extendedCatcher struct{ *baseCatcher }
func (c *extendedCatcher) String() string {
c.mutex.RLock()
defer c.mutex.RUnlock()
output := make([]string, len(c.errs))
for idx, err := range c.errs {
output[idx] = fmt.Sprintf("%+v", err)
}
return strings.Join(output, "\n")
}
type simpleCatcher struct{ *baseCatcher }
func (c *simpleCatcher) String() string {
c.mutex.RLock()
defer c.mutex.RUnlock()
output := make([]string, len(c.errs))
for idx, err := range c.errs {
output[idx] = fmt.Sprintf("%s", err)
}
return strings.Join(output, "\n")
}
type basicCatcher struct{ *baseCatcher }
func (c *basicCatcher) String() string {
c.mutex.RLock()
defer c.mutex.RUnlock()
output := make([]string, len(c.errs))
for idx, err := range c.errs {
output[idx] = err.Error()
}
return strings.Join(output, "\n")
}
////////////////////////////////////////////////////////////////////////
//
// an implementation to annotate errors with timestamps
type timeAnnotatingCatcher struct {
errs []*timestampError
extended bool
mu sync.RWMutex
}
// NewTimestampCatcher produces a Catcher instance that reports the
// short form of all constituent errors and annotates those errors
// with a timestamp to reflect when the error was collected.
func NewTimestampCatcher() Catcher { return &timeAnnotatingCatcher{} }
// NewExtendedTimestampCatcher adds long-form annotation to the
// aggregated error message (e.g. including stacks, when possible.)
func NewExtendedTimestampCatcher() Catcher { return &timeAnnotatingCatcher{extended: true} }
func (c *timeAnnotatingCatcher) Add(err error) {
if err == nil {
return
}
c.mu.Lock()
defer c.mu.Unlock()
tserr := newTimeStampError(err)
if tserr == nil {
return
}
c.errs = append(c.errs, tserr.setExtended(c.extended))
}
func (c *timeAnnotatingCatcher) Extend(errs []error) {
if len(errs) == 0 {
return
}
c.mu.Lock()
defer c.mu.Unlock()
for _, err := range errs {
if err == nil {
continue
}
c.errs = append(c.errs, newTimeStampError(err).setExtended(c.extended))
}
}
func (c *timeAnnotatingCatcher) AddWhen(cond bool, err error) {
if !cond {
return
}
c.Add(err)
}
func (c *timeAnnotatingCatcher) ExtendWhen(cond bool, errs []error) {
if !cond {
return
}
c.Extend(errs)
}
func (c *timeAnnotatingCatcher) New(e string) {
if e == "" {
return
}
c.Add(errors.New(e))
}
func (c *timeAnnotatingCatcher) NewWhen(cond bool, e string) {
if !cond {
return
}
c.New(e)
}
func (c *timeAnnotatingCatcher) Errorf(f string, args ...interface{}) {
if f == "" {
return
} else if len(args) == 0 {
c.New(f)
return
}
c.Add(errors.Errorf(f, args...))
}
func (c *timeAnnotatingCatcher) ErrorfWhen(cond bool, f string, args ...interface{}) {
if !cond {
return
}
c.Errorf(f, args...)
}
func (c *timeAnnotatingCatcher) Wrap(err error, m string) {
c.Add(WrapErrorTimeMessage(err, m))
}
func (c *timeAnnotatingCatcher) Wrapf(err error, f string, args ...interface{}) {
c.Add(WrapErrorTimeMessagef(err, f, args...))
}
func (c *timeAnnotatingCatcher) Check(fn CheckFunction) {
c.Add(fn())
}
func (c *timeAnnotatingCatcher) CheckWhen(cond bool, fn CheckFunction) {
if !cond {
return
}
c.Add(fn())
}
func (c *timeAnnotatingCatcher) CheckExtend(fns []CheckFunction) {
for _, fn := range fns {
c.Add(fn())
}
}
func (c *timeAnnotatingCatcher) Len() int {
c.mu.RLock()
defer c.mu.RUnlock()
return len(c.errs)
}
func (c *timeAnnotatingCatcher) HasErrors() bool {
c.mu.RLock()
defer c.mu.RUnlock()
return len(c.errs) > 0
}
func (c *timeAnnotatingCatcher) Errors() []error {
c.mu.RLock()
defer c.mu.RUnlock()
out := make([]error, len(c.errs))
for idx, err := range c.errs {
out[idx] = err
}
return out
}
func (c *timeAnnotatingCatcher) String() string {
c.mu.RLock()
defer c.mu.RUnlock()
output := make([]string, len(c.errs))
for idx, err := range c.errs {
if err.extended {
output[idx] = err.String()
} else {
output[idx] = err.String()
}
}
return strings.Join(output, "\n")
}
func (c *timeAnnotatingCatcher) Resolve() error {
if !c.HasErrors() {
return nil
}
return errors.New(c.String())
}