-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_wrap.go
76 lines (69 loc) · 1.75 KB
/
error_wrap.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
package goapperrors
import (
"errors"
"fmt"
"runtime"
goerrors "github.com/go-errors/errors"
)
// Wrap wraps an error with adding stack trace if configured
func Wrap(err error) error {
if globalConfig.WrapFunc != nil {
return globalConfig.WrapFunc(err)
}
return goerrors.Wrap(err, 1)
}
// Wrapf wraps an error by calling fmt.Errorf and adds stack trace if configured
func Wrapf(format string, args ...any) error {
return Wrap(fmt.Errorf(format, args...)) //nolint:err113
}
// GetStackTrace gets stack trace stored in the error if there is
func GetStackTrace(err error) []runtime.Frame {
if err == nil {
return nil
}
// If it is *go-errors.Error, get the stack trace from it
var gErr *goerrors.Error
if !errors.As(err, &gErr) {
return nil
}
callers := gErr.Callers()
frames := runtime.CallersFrames(callers)
frameList := make([]runtime.Frame, 0, len(callers))
for {
next, hasMore := frames.Next()
frameList = append(frameList, next)
if !hasMore {
break
}
}
return frameList
}
// UnwrapToRoot keeps unwrapping until the root error
func UnwrapToRoot(err error) error {
lastErr := err
for {
e := errors.Unwrap(lastErr)
if e == nil {
return lastErr
}
lastErr = e
}
}
// UnwrapMulti unwraps en error to a slice of errors.
// If the error implements `Unwrap() []error`, the result of func call is returned.
// If the error implements `Unwrap() error`, the func is called and the result slice
// has only one element.
// If no `Unwrap` func is implemented in the error, `nil` is returned.
func UnwrapMulti(err error) []error {
if u, ok := err.(interface{ Unwrap() []error }); ok {
return u.Unwrap()
}
if u, ok := err.(interface{ Unwrap() error }); ok {
e := u.Unwrap()
if e == nil {
return nil
}
return []error{e}
}
return nil
}