-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultierr.go
191 lines (165 loc) · 4.06 KB
/
multierr.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
package errors
import (
std "errors"
"io"
"io/ioutil"
)
// MultiError interface for compatibility with errors from the library
// https://github.com/uber-go/multierr.
type MultiError interface {
Errors() []error
}
// Errors returns a copy of the list of combined errors.
func (e *Error) Errors() []error {
// e.errs must be immutable.
errs := make([]error, len(e.errs))
copy(errs, e.errs)
return errs
}
// Errors returns a list of errors if combined. Compatible with
// https://github.com/uber-go/multierr.
// for _, err := errors.Errors(err) {
// fmt.Println(err)
// }
func Errors(err error) (errs []error) {
return global.Errors(err)
}
func (b Builder) Errors(err error) (errs []error) {
r := b.extractFrom(err)
if r == nil {
return nil
}
return r.errs
}
// Combine combines several errors into one.
// errors.Combine(err1, err2)
func Combine(errs ...error) error {
return global.WithSkip(1).Combine(errs...)
}
func (b Builder) Combine(errs ...error) error {
var n int
for _, err := range errs {
if err != nil {
errs[n], n = err, n+1
}
}
errs = errs[:n]
if len(errs) == 0 {
return nil
}
var (
r = Error{
errs: make([]error, 0, len(errs)),
formats: make([]FormatArgs, 0, len(errs)),
ReasonType: b.ReasonType,
ExtraFields: ExtraFields{},
}
nested *Error
multiError MultiError
ok bool
)
for _, err := range errs {
if std.As(err, &nested) {
if !b.OverwriteReason &&
r.ReasonType == ReasonInternal &&
nested.ReasonType != ReasonInternal {
r.ReasonType = nested.ReasonType
}
for field, v := range nested.ExtraFields {
r.ExtraFields[field] = v
}
// Take a stack of nested errors, if necessary.
if len(r.callers) == 0 && len(nested.callers) > 0 {
r.callers = nested.callers
}
r.errs, r.formats =
append(r.errs, nested.errs...),
append(r.formats, nested.formats...)
continue
}
if multiError, ok = err.(MultiError); !ok {
r.errs, r.formats =
append(r.errs, err),
append(
r.formats, FormatArgs{
Format: "%v",
Args: []interface{}{err},
},
)
continue
}
// Compatibility with https://github.com/uber-go/multierr.
for _, err = range multiError.Errors() {
if err == nil {
continue
}
r.errs, r.formats =
append(r.errs, err),
append(
r.formats, FormatArgs{
Format: "%v",
Args: []interface{}{err},
},
)
}
}
// Take a stack if needed.
if len(r.callers) == 0 {
r.callers = b.CallersIfNeed()
}
return &r
}
// Append combines two errors into one.
// errors.Append(err1, err2)
func Append(left, right error) error {
return global.WithSkip(1).Append(left, right)
}
func (b Builder) Append(left, right error) error {
return b.WithSkip(1).Combine(left, right)
}
// AppendInto puts err at into and returns true if err != nil, must be into != nil.
// if errors.AppendInto(&err, rows.Close) {
// return err
// }
//nolint:gocritic
func AppendInto(into *error, err error) (errored bool) {
return global.WithSkip(1).AppendInto(into, err)
}
//nolint:gocritic
func (b Builder) AppendInto(into *error, err error) (errored bool) {
if into != nil && err != nil {
*into = b.WithSkip(1).Combine(*into, err)
}
return err != nil
}
// CloseAndAppendInto closes c and puts err at into and returns true
// if c.Close() != nil, must be into != nil.
// if errors.CloseAndAppendInto(&err, resp.Body) {
// return err
// }
//nolint:gocritic
func CloseAndAppendInto(into *error, c io.Closer) (errored bool) {
if c == nil {
return false
}
// This will free up the reader in some cases, for example, for
// the body of the http response. See
// https://github.com/golang/go/blob/release-branch.go1.16/src/net/http/transport.go#L2204
if r, ok := c.(io.Reader); ok {
_, err := io.Copy(ioutil.Discard, r)
errored = global.WithSkip(1).AppendInto(
into, WithStackCustom(
global.WithSkip(1).Wrapf(err, "reading %#T", r),
1,
2,
),
)
}
return errored || global.WithSkip(1).AppendInto(
into, WithStackCustom(
global.WithSkip(1).Wrapf(c.Close(), "closing %#T", c),
1,
2,
),
)
}