forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 19
/
error_recorder.go
124 lines (109 loc) · 2.88 KB
/
error_recorder.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
// Copyright 2013, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package concurrency
import (
"fmt"
"strings"
"sync"
log "github.com/golang/glog"
)
// ErrorRecorder offers a way to record errors during complex
// asynchronous operations. Various implementation will offer
// different services.
type ErrorRecorder interface {
RecordError(error)
HasErrors() bool
Error() error
}
// FirstErrorRecorder records the first error, logs the others.
// Error() will return the first recorded error or nil.
type FirstErrorRecorder struct {
mu sync.Mutex
errorCount int
firstError error
}
// RecordError records a possible error:
// - does nothing if err is nil
// - only records the first error reported
// - the rest is just logged
func (fer *FirstErrorRecorder) RecordError(err error) {
if err == nil {
return
}
fer.mu.Lock()
fer.errorCount++
if fer.errorCount == 1 {
fer.firstError = err
} else {
log.Errorf("FirstErrorRecorder: error[%v]: %v", fer.errorCount, err)
}
fer.mu.Unlock()
}
// HasErrors returns true if we ever recorded an error
func (fer *FirstErrorRecorder) HasErrors() bool {
fer.mu.Lock()
defer fer.mu.Unlock()
return fer.errorCount > 0
}
// Error returns the first error we saw, or nil
func (fer *FirstErrorRecorder) Error() error {
fer.mu.Lock()
defer fer.mu.Unlock()
return fer.firstError
}
// AllErrorRecorder records all the errors.
type AllErrorRecorder struct {
mu sync.Mutex
Errors []error
}
// RecordError records a possible error:
// - does nothing if err is nil
func (aer *AllErrorRecorder) RecordError(err error) {
if err == nil {
return
}
aer.mu.Lock()
aer.Errors = append(aer.Errors, err)
aer.mu.Unlock()
}
// HasErrors returns true if we ever recorded an error
func (aer *AllErrorRecorder) HasErrors() bool {
aer.mu.Lock()
defer aer.mu.Unlock()
return len(aer.Errors) > 0
}
type AllErrorAggregator func(errors []error) error
// AggrError() runs the provided aggregator over all errors
// and returns the error from aggregator.
func (aer *AllErrorRecorder) AggrError(aggr AllErrorAggregator) error {
aer.mu.Lock()
defer aer.mu.Unlock()
if len(aer.Errors) == 0 {
return nil
}
return aggr(aer.Errors)
}
// Error() returns an aggregate of all errors by concatenation.
func (aer *AllErrorRecorder) Error() error {
return aer.AggrError(func(errors []error) error {
errs := make([]string, 0, len(aer.Errors))
for _, e := range aer.Errors {
errs = append(errs, e.Error())
}
return fmt.Errorf("%v", strings.Join(errs, "\n"))
})
}
// ErrorStrings() returns all errors as string array.
func (aer *AllErrorRecorder) ErrorStrings() []string {
aer.mu.Lock()
defer aer.mu.Unlock()
if len(aer.Errors) == 0 {
return nil
}
errs := make([]string, 0, len(aer.Errors))
for _, e := range aer.Errors {
errs = append(errs, e.Error())
}
return errs
}