forked from OneOfOne/oerrs
/
errorlist.go
201 lines (167 loc) · 3.42 KB
/
errorlist.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
package oerrs
import (
"errors"
"fmt"
"strings"
"sync"
)
func NewList() *ErrorList {
return &ErrorList{}
}
func NewSafeList(includeCallers bool) *ErrorList {
var mux sync.RWMutex
return &ErrorList{
mux: &mux,
}
}
type ErrorList struct {
mux *sync.RWMutex
errs []error
}
func (e *ErrorList) Safe() {
if e.mux != nil {
panic("race")
}
e.mux = new(sync.RWMutex)
}
func (e *ErrorList) Errorf(format string, args ...interface{}) {
err := Errorf(format, args...)
defer e.lock(true)()
if AlwaysWithCaller {
err = &wrapped{err, Caller(1)}
}
e.errs = append(e.errs, err)
}
func (e *ErrorList) ErrorCallerf(skip int, format string, args ...interface{}) {
err := ErrorCallerf(skip+1, format, args...)
defer e.lock(true)()
e.errs = append(e.errs, err)
}
func (e *ErrorList) PushIf(err error) bool {
if err == nil {
return false
}
if AlwaysWithCaller {
err = &wrapped{err, Caller(2)}
}
defer e.lock(true)()
e.errs = append(e.errs, err)
return true
}
func (e *ErrorList) PushCallerIf(err error, skip int) bool {
if err == nil {
return false
}
err = &wrapped{err, Caller(skip + 1)}
defer e.lock(true)()
e.errs = append(e.errs, err)
return true
}
func (e *ErrorList) Reset() {
if e == nil {
return
}
defer e.lock(true)()
e.errs = nil
}
func (e *ErrorList) Err() error {
if e == nil {
return nil
}
defer e.lock(false)()
if len(e.errs) == 0 {
return nil
}
return e
}
// Error implements the error interface
func (e *ErrorList) Error() string {
defer e.lock(false)()
return fmtErrors(e.errs, AlwaysWithCaller)
}
// Unwrap implements errors.Unwrap by returning the next error in the
// chain or nil if there are no more errors.
func (e *ErrorList) Unwrap() error {
defer e.lock(false)()
if len(e.errs) == 1 {
return nil
}
mux := e.mux
if mux != nil {
mux = new(sync.RWMutex)
}
return &ErrorList{errs: e.errs[1:], mux: mux}
}
// As implements errors.As by checking if target is ErrorList, otherwise will go through each error.
func (e *ErrorList) As(target interface{}) bool {
defer e.lock(false)()
if oe, ok := target.(*ErrorList); ok {
defer oe.lock(true)()
oe.errs = append([]error(nil), e.errs...)
return true
}
for _, err := range e.errs {
if errors.As(err, target) {
return true
}
}
return false
}
// Is implements errors.Is by comparing if target is ErrorList, otherwise will go through each error.
func (e *ErrorList) Is(target error) bool {
defer e.lock(false)()
if oe, ok := target.(*ErrorList); ok {
defer oe.lock(false)()
if len(e.errs) != len(oe.errs) {
return false
}
for i, err := range e.errs {
if !Is(err, oe.errs[i]) {
return false
}
}
return true
}
for _, err := range e.errs {
if Is(err, target) {
return true
}
}
return false
}
func (e *ErrorList) Errors() []error {
defer e.lock(false)()
errs := make([]error, 0, len(e.errs))
return append(errs, e.errs...)
}
func (e *ErrorList) lock(rw bool) func() {
if e == nil || e.mux == nil {
return func() {}
}
mux := e.mux
if rw {
mux.Lock()
return mux.Unlock
}
mux.RLock()
return mux.RUnlock
}
func fmtErrors(es []error, withStack bool) string {
if len(es) == 0 {
return ""
}
verb := "%v"
if withStack {
verb = "%+v"
}
if len(es) == 1 {
return fmt.Sprintf(verb, es[0])
}
var buf strings.Builder
fmt.Fprintf(&buf, "%d errors occurred:", len(es))
for _, err := range es {
buf.WriteString("\n\t* ")
fmt.Fprintf(&buf, verb, err)
}
return buf.String()
}