-
Notifications
You must be signed in to change notification settings - Fork 13
/
errs.go
287 lines (246 loc) · 7.71 KB
/
errs.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package errs
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/ActiveState/cli/internal/condition"
"github.com/ActiveState/cli/internal/osutils/stacktrace"
"github.com/ActiveState/cli/internal/rtutils"
"gopkg.in/yaml.v3"
)
const TipMessage = "wrapped tips"
type AsError interface {
As(interface{}) bool
}
// WrapperError enforces errors that include a stacktrace
type Errorable interface {
Unwrap() error
Stack() *stacktrace.Stacktrace
}
type ErrorTips interface {
error
AddTips(...string)
ErrorTips() []string
}
// TransientError represents an error that is transient, meaning it does not itself represent a failure, but rather it
// facilitates a mechanic meant to get to the actual error (eg. by wrapping or packing underlying errors).
// Do NOT satisfy this interface for errors whose type you want to assert.
type TransientError interface {
IsTransient()
}
// PackedErrors represents a collection of errors that aren't necessarily related to each other
// note that rtutils replicates this functionality to avoid import cycles
type PackedErrors struct {
errors []error
}
func (e *PackedErrors) IsTransient() {}
func (e *PackedErrors) Error() string {
return "packed multiple errors"
}
func (e *PackedErrors) Unwrap() []error {
return e.errors
}
// WrapperError is what we use for errors created from this package, this does not mean every error returned from this
// package is wrapping something, it simply has the plumbing to.
type WrapperError struct {
message string
tips []string
wrapped error
stack *stacktrace.Stacktrace
}
func (e *WrapperError) Error() string {
return e.message
}
func (e *WrapperError) ErrorTips() []string {
return e.tips
}
func (e *WrapperError) AddTips(tips ...string) {
e.tips = append(e.tips, tips...)
}
// Unwrap returns the parent error, if one exists
func (e *WrapperError) Unwrap() error {
return e.wrapped
}
// Stack returns the stacktrace for where this error was created
func (e *WrapperError) Stack() *stacktrace.Stacktrace {
return e.stack
}
func newError(message string, wrapTarget error) *WrapperError {
return &WrapperError{
message,
[]string{},
wrapTarget,
stacktrace.GetWithSkip([]string{rtutils.CurrentFile()}),
}
}
// New creates a new error, similar to errors.New
func New(message string, args ...interface{}) *WrapperError {
msg := fmt.Sprintf(message, args...)
return newError(msg, nil)
}
// Wrap creates a new error that wraps the given error
func Wrap(wrapTarget error, message string, args ...interface{}) *WrapperError {
msg := fmt.Sprintf(message, args...)
return newError(msg, wrapTarget)
}
// Pack creates a new error that packs the given errors together, allowing for multiple errors to be returned
func Pack(err error, errs ...error) error {
return &PackedErrors{append([]error{err}, errs...)}
}
// encodeErrorForJoin will recursively encode an error into a format that can be marshalled in a way that is easily
// humanly readable.
// In a nutshell the logic is:
// - If the error is nil, return nil
// - If the error is wrapped other errors, return it as a map with the key being the error and the value being the wrapped error(s)
// - If the error is packing other errors, return them as a list of errors
func encodeErrorForJoin(err error) interface{} {
if err == nil {
return nil
}
// If the error is a wrapper, unwrap it and encode the wrapped error
if u, ok := err.(unwrapNext); ok {
subErr := u.Unwrap()
if subErr == nil {
return err.Error()
}
return map[string]interface{}{err.Error(): encodeErrorForJoin(subErr)}
}
// If the error is a packer, encode the packed errors as a list
if u, ok := err.(unwrapPacked); ok {
var result []interface{}
// Don't encode errors that are transient as the real errors are kept underneath
if _, isTransient := err.(TransientError); !isTransient {
result = append(result, err.Error())
}
errs := u.Unwrap()
for _, nextErr := range errs {
result = append(result, encodeErrorForJoin(nextErr))
}
if len(result) == 1 {
return result[0]
}
return result
}
return err.Error()
}
func JoinMessage(err error) string {
v, err := yaml.Marshal(encodeErrorForJoin(err))
if err != nil {
// This shouldn't happen since we know exactly what we are marshalling
return fmt.Sprintf("failed to marshal error: %s", err)
}
return strings.TrimSpace(string(v))
}
func AddTips(err error, tips ...string) error {
var errTips ErrorTips
// MultiError uses a custom type to wrap multiple errors, so the type casting above won't work.
// Instead it satisfied `errors.As()`, but here we want to specifically check the current error and not any wrapped errors.
if asError, ok := err.(AsError); ok {
asError.As(&errTips)
}
if _, ok := err.(ErrorTips); ok {
errTips = err.(ErrorTips)
}
if errTips == nil {
// use original error message with identifier in case this bubbles all the way up
// this helps us identify it on rollbar without affecting the UX too much
errTips = newError(TipMessage, err)
err = errTips
}
errTips.AddTips(tips...)
return err
}
var errorType = reflect.TypeOf((*error)(nil)).Elem()
// Matches is an analog for errors.As that just checks whether err matches the given type, so you can do:
// errs.Matches(err, &ErrStruct{})
// Without having to first assign it to a variable
// This is useful if you ONLY care about the bool return value and not about setting the variable
func Matches(err error, target interface{}) bool {
if target == nil {
panic("errors: target cannot be nil")
}
// Guard against miss-use of this function
if _, ok := target.(*WrapperError); ok {
if condition.BuiltOnDevMachine() || condition.InActiveStateCI() {
panic("target cannot be a WrapperError, you probably want errors.Is")
}
}
val := reflect.ValueOf(target)
targetType := val.Type()
if targetType.Kind() != reflect.Interface && !targetType.Implements(errorType) {
panic("errors: *target must be interface or implement error")
}
errs := Unpack(err)
for _, err := range errs {
if reflect.TypeOf(err).AssignableTo(targetType) {
return true
}
if x, ok := err.(interface{ As(interface{}) bool }); ok && x.As(&target) {
return true
}
}
return false
}
func IsAny(err error, errs ...error) bool {
for _, e := range errs {
if errors.Is(err, e) {
return true
}
}
return false
}
type unwrapNext interface {
Unwrap() error
}
type unwrapPacked interface {
Unwrap() []error
}
// Unpack will recursively unpack an error into a list of errors, which is useful if you need to iterate over all errors.
// This is similar to errors.Unwrap, but will also "unwrap" errors that are packed together, which errors.Unwrap does not.
func Unpack(err error) []error {
result := []error{}
// add is a little helper function to add errors to the result, skipping any transient errors
add := func(errors ...error) {
for _, err := range errors {
if _, isTransient := err.(TransientError); isTransient {
continue
}
result = append(result, err)
}
}
// recursively unpack the error
for err != nil {
add(err)
if u, ok := err.(unwrapNext); ok {
// The error implements `Unwrap() error`, so simply unwrap it and continue the loop
err = u.Unwrap() // The next iteration will add the error to the result
continue
} else if u, ok := err.(unwrapPacked); ok {
// The error implements `Unwrap() []error`, so just add the resulting errors to the result and break the loop
errs := u.Unwrap()
for _, e := range errs {
add(Unpack(e)...)
}
break
} else {
break // nothing to unpack
}
}
return result
}
type ExternalError interface {
ExternalError() bool
}
func IsExternalError(err error) bool {
if err == nil {
return false
}
for _, err := range Unpack(err) {
errExternal, ok := err.(ExternalError)
if ok && errExternal.ExternalError() {
return true
}
}
return false
}