-
Notifications
You must be signed in to change notification settings - Fork 179
/
errors_collector.go
60 lines (47 loc) · 1.15 KB
/
errors_collector.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
package errors
import (
"fmt"
"github.com/hashicorp/go-multierror"
)
// Utility object for collecting Processor errors.
type ErrorsCollector struct {
errors error
failure error
}
func NewErrorsCollector() *ErrorsCollector {
return &ErrorsCollector{}
}
func (collector *ErrorsCollector) CollectedFailure() bool {
return collector.failure != nil
}
func (collector *ErrorsCollector) CollectedError() bool {
return collector.errors != nil || collector.failure != nil
}
func (collector *ErrorsCollector) ErrorOrNil() error {
if collector.failure == nil {
return collector.errors
}
if collector.errors == nil {
return collector.failure
}
return fmt.Errorf(
"%w (additional errors: %v)",
collector.failure,
collector.errors)
}
// This returns collector for chaining purposes.
func (collector *ErrorsCollector) Collect(err error) *ErrorsCollector {
if err == nil {
return collector
}
if collector.failure != nil {
collector.errors = multierror.Append(collector.errors, err)
return collector
}
if IsFailure(err) {
collector.failure = err
return collector
}
collector.errors = multierror.Append(collector.errors, err)
return collector
}