-
Notifications
You must be signed in to change notification settings - Fork 0
/
errorwrap.go
347 lines (303 loc) · 8.13 KB
/
errorwrap.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package errorwrap
import (
"errors"
"fmt"
"io"
)
var (
multilineSeparator = " - "
multilineIndent = " "
)
// ErrorDefinition is a definition of an error. It is used like an immutable variable.
type ErrorDefinition interface {
error
fmt.Formatter
}
// ErrorWrapper is a multi level error. It is built like a stack (vertical from bottom to top) that contains multiple
// ErrorDefinition (horizontal) in each level. If there is a 3 level, then each level will have an ErrorWrapper
// instance with one ErrorDefinition (CurrentError) minimum.
//
// ErrorWrapper Z (CurrentError contains any objects that implement error, RootCause contains ErrorWrapper X, ParentError contains ErrorWrapper Y)
// V
// ErrorWrapper Y (CurrentError contains any objects that implement error, RootCause contains ErrorWrapper X, ParentError contains ErrorWrapper X)
// V
// ErrorWrapper X (CurrentError contains any objects that implement error, RootCause contains nil, ParentError contains nil)
//
type ErrorWrapper interface {
// CurrentError represents as the current level error (the top level of the stack).
CurrentError() []error
// ContextMessage is used as a modifier to the CurrentError. It is used for elaborating the CurrentError.
ContextMessage() string
// RootCause will return the bottom level of the stack.
RootCause() ErrorWrapper
// ParentError will return an ErrorWrapper below the current level.
ParentError() ErrorWrapper
// StackTrace return a StackTrace of the current ErrorWrapper level only.
StackTrace() StackTrace
error
Unwrap() error
Is(target error) bool
As(target interface{}) bool
fmt.Formatter
}
type errorDefinition struct {
msg string
}
func (e *errorDefinition) Error() string {
return e.msg
}
func (e *errorDefinition) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
fallthrough
case 's':
io.WriteString(s, e.Error())
case 'q':
fmt.Fprintf(s, "%q", e.Error())
}
}
type errorWrapper struct {
errors []error
contextMsg string
rootCause ErrorWrapper
parentError ErrorWrapper
*stack
}
func (e *errorWrapper) CurrentError() []error {
return e.errors
}
func (e *errorWrapper) ContextMessage() string {
return e.contextMsg
}
func (e *errorWrapper) RootCause() ErrorWrapper {
return e.rootCause
}
func (e *errorWrapper) ParentError() ErrorWrapper {
return e.parentError
}
func (e *errorWrapper) StackTrace() StackTrace {
return e.stack.StackTrace()
}
func (e *errorWrapper) Error() string {
str := ""
if len(e.errors) <= 0 {
return ""
} else if len(e.errors) == 1 {
str += multilineSeparator + e.errors[0].Error()
} else {
for i, err := range e.errors {
switch i {
case 0:
str += multilineSeparator + err.Error()
default:
str += "\n" + multilineIndent + err.Error()
}
}
}
if e.contextMsg != "" {
str += "\n" + multilineIndent + "context: " + e.contextMsg
}
return str
}
func (e *errorWrapper) fullError() string {
str := e.Error()
if e.parentError != nil {
str += "\n"
if ec, ok := e.parentError.(*errorWrapper); ok && ec != nil {
str += ec.fullError()
} else {
str += e.parentError.Error()
}
}
return str
}
func (e *errorWrapper) Unwrap() error {
return e.parentError
}
func (e *errorWrapper) Is(target error) bool {
for _, err := range e.errors {
if errors.Is(err, target) {
return true
}
}
return false
}
func (e *errorWrapper) As(target interface{}) bool {
if target == nil {
if e.errors == nil {
return true
}
return false
}
for _, err := range e.errors {
if errors.As(err, target) {
return true
}
}
return false
}
func (e *errorWrapper) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v\n\n", e.fullError())
if rc, ok := e.rootCause.(*errorWrapper); ok && rc != nil {
rc.stack.Format(s, verb)
} else {
e.stack.Format(s, verb)
}
return
}
fallthrough
case 's':
if s.Flag('+') {
io.WriteString(s, e.fullError())
return
}
io.WriteString(s, e.Error())
case 'q':
fmt.Fprintf(s, "%q", e.Error())
}
}
// New creates an ErrorDefinition
func New(message string) error {
return &errorDefinition{
msg: message,
}
}
func newErrorWrapper(err ...error) *errorWrapper {
var errs []error
for _, e := range err {
if e == nil {
continue
}
errs = append(errs, e)
}
if len(errs) == 0 {
return nil
}
return &errorWrapper{
errors: errs,
}
}
// NewError creates a base or root ErrorWrapper.
//
// ErrorWrapper.CurrentError is populated with err. If err is nil then NewError returns nil.
// It is recommended to pass ErrorDefinition as err arguments.
func NewError(err ...error) error {
errWrap := newErrorWrapper(err...)
if errWrap == nil {
return nil
}
errWrap.stack = callStack()
return errWrap
}
// NewErrorWithMessage creates a base or root ErrorWrapper.
//
// ErrorWrapper.CurrentError is populated with err.
// ErrorWrapper.ContextMessage is populated with contextMessage.
// It is recommended to pass ErrorDefinition as err arguments.
func NewErrorWithMessage(contextMessage string, err ...error) error {
errWrap := newErrorWrapper(err...)
if errWrap == nil {
return nil
}
errWrap.stack = callStack()
errWrap.contextMsg = contextMessage
return errWrap
}
// AppendInto appends err into errWrapper. It will return the same errWrapper instance or new instance if errWrapper is nil.
// It is recommended to pass ErrorDefinition as err arguments.
func AppendInto(errWrapper error, err ...error) error {
ew, ok := errWrapper.(*errorWrapper)
if ok && ew != nil {
ew.errors = append(ew.errors, err...)
} else {
ew = newErrorWrapper(err...)
if ew == nil {
return nil
}
ew.stack = callStack()
}
return ew
}
// Wrap returns an ErrorWrapper{CurrentError: wrapper, ParentError: parent, RootCause: parent.RootCause}.
// It is recommended to pass ErrorDefinition as err arguments.
func Wrap(parent error, err ...error) error {
ec := WrapWithMessage(parent, "", err...)
if ec == nil {
return nil
}
if p, ok := parent.(*errorWrapper); !ok || p == nil {
if parentErr, ok2 := ec.(*errorWrapper).parentError.(*errorWrapper); ok2 && parentErr != nil {
parentErr.stack = callStack()
}
}
ec.(*errorWrapper).stack = callStack()
return ec
}
// WrapWithMessage returns an ErrorWrapper{CurrentError: wrapper, ParentError: parent, RootCause: parent.RootCause, ContextMessage: contextMessage}.
// It is recommended to pass ErrorDefinition as err arguments.
func WrapWithMessage(parent error, contextMessage string, err ...error) error {
if parent == nil && len(err) == 0 {
return nil
}
parentWrapper, ok := parent.(*errorWrapper)
if !ok {
parentWrapper = newErrorWrapper(parent)
if parentWrapper != nil {
parentWrapper.stack = callStack()
}
}
currError := newErrorWrapper(err...)
if currError == nil {
if parentWrapper != nil {
return parentWrapper
}
return nil
}
if parentWrapper != nil {
if parentWrapper.rootCause == nil {
currError.rootCause = parentWrapper
} else {
currError.rootCause = parentWrapper.rootCause
}
currError.parentError = parentWrapper
}
currError.contextMsg = contextMessage
currError.stack = callStack()
return currError
}
// Wrapper returns ErrorWrapper instance of err in target level
func Wrapper(err error, target error) ErrorWrapper {
if target == nil || err == nil {
return nil
}
for {
if curWrapper, ok := err.(ErrorWrapper); ok && (curWrapper == target || curWrapper.Is(target)) {
return curWrapper
}
if err = Unwrap(err); err == nil {
return nil
}
}
}
// Is checks whether target is placed in err (ErrorWrapper) or not. It will find recursively from current level
// to the root of ErrorWrapper stack.
func Is(err error, target error) bool {
return errors.Is(err, target)
}
// IsExact checks whether target is placed in current level error of err (ErrorWrapper) or not.
func IsExact(err error, target error) bool {
if curWrapper, ok := err.(ErrorWrapper); ok && (curWrapper == target || curWrapper.Is(target)) {
return true
}
return false
}
func As(err error, target interface{}) bool {
return errors.As(err, target)
}
// Unwrap will return the parent error (ErrorWrapper) of err (ErrorWrapper)
func Unwrap(err error) error {
return errors.Unwrap(err)
}