-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
90 lines (72 loc) · 1.46 KB
/
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
package pick
import (
"fmt"
"strings"
)
type multiError struct {
errors []error
}
func (e *multiError) Error() string {
if e == nil || len(e.errors) == 0 {
return ""
}
s := strings.Builder{}
for i, err := range e.errors {
if i != 0 {
s.WriteString(" | ")
}
s.WriteString(err.Error())
}
return s.String()
}
func (e *multiError) Add(err error) {
e.errors = append(e.errors, err)
}
func (e *multiError) Unwrap() []error {
return e.errors
}
func gather(dst *error, newErr error) {
if newErr == nil {
return
}
if dst == nil {
return
}
var gatherer *multiError
if *dst == nil {
gatherer = &multiError{}
*dst = gatherer
} else if g, is := (*dst).(*multiError); is { //nolint:errorlint // we need to check only the top layer.
gatherer = g
} else {
gatherer = &multiError{}
gatherer.Add(*dst)
*dst = gatherer
}
gatherer.Add(newErr)
}
type ErrorsSink struct {
err error
}
func (e *ErrorsSink) GatherSelector(selector string, err error) {
gather(&e.err, &PickerError{selector: selector, inner: err})
}
func (e *ErrorsSink) Gather(err error) {
gather(&e.err, err)
}
func (e *ErrorsSink) Outcome() error {
return e.err
}
type PickerError struct {
inner error
selector string
}
func (e *PickerError) Selector() string {
return e.selector
}
func (e *PickerError) Error() string {
return fmt.Sprintf("picker error with selector `%s` error: `%s`", e.selector, e.inner.Error())
}
func (e *PickerError) Unwrap() error {
return e.inner
}