-
Notifications
You must be signed in to change notification settings - Fork 4
/
export.go
180 lines (154 loc) · 3.97 KB
/
export.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
package errors
import (
"fmt"
"reflect"
std_caller "github.com/bdlm/std/v2/caller"
std_error "github.com/bdlm/std/v2/errors"
)
var errorType = reflect.TypeOf((*error)(nil)).Elem()
// As searches the error stack for an error that can be cast to the test
// argument, which must be a pointer. If it succeeds it performs the
// assignment and returns the result, otherwise it returns nil.
func As(err, test error) error {
if nil == err || nil == test {
return nil
}
val := reflect.ValueOf(test)
typ := val.Type()
if typ.Kind() != reflect.Ptr || val.IsNil() {
return nil
}
if e := typ.Elem(); e.Kind() != reflect.Interface && !e.Implements(errorType) {
return nil
}
testType := typ.Elem()
for err != nil {
if reflect.TypeOf(err).AssignableTo(testType) {
val.Elem().Set(reflect.ValueOf(err))
return err
}
if e, ok := err.(interface{ As(error) error }); ok {
return e.As(test)
}
err = Unwrap(err)
}
return nil
}
// Caller returns the Caller associated with an error, if any.
func Caller(err error) std_caller.Caller {
if e, ok := err.(std_error.Caller); ok {
return e.Caller()
}
return nil
}
// Errorf formats according to a format specifier and returns an error that
// contains caller data.
func Errorf(msg string, data ...interface{}) *E {
return New(fmt.Sprintf(msg, data...))
}
// Is reports whether any error in err's chain matches test.
//
// The chain consists of err itself followed by the sequence of errors obtained by
// repeatedly calling Unwrap.
//
// An error is considered to match a test if it is equal to that test or if
// it implements a method Is(error) bool such that Is(test) returns true.
//
// An error type might provide an Is method so it can be treated as equivalent
// to an existing error. For example, if MyError defines
//
// func (m MyError) Is(test error) bool { return test == os.ErrExist }
//
// then Is(MyError{}, os.ErrExist) returns true. See syscall.Errno.Is for
// an example in the standard library.
func Is(err, test error) bool {
if nil == err || nil == test {
return false
}
isComparable := reflect.TypeOf(err).Comparable() && reflect.TypeOf(test).Comparable()
if isComparable && err == test {
return true
}
if e, ok := err.(*E); ok {
isComparable := reflect.TypeOf(e.err).Comparable() && reflect.TypeOf(test).Comparable()
if isComparable && e.err == test {
return true
}
}
if e, ok := err.(interface{ Is(error) bool }); ok {
return e.Is(test)
}
if err = Unwrap(err); err == nil {
return false
}
return Is(err, test)
}
// New returns an error that contains caller data.
func New(msg string) *E {
return &E{
caller: NewCaller(),
err: fmt.Errorf(msg),
}
}
// Trace adds an additional caller line to the error trace trace on an error
// to aid in debugging and forensic analysis.
func Trace(e error) *E {
if nil == e {
return nil
}
clr := NewCaller().(*caller)
clr.trace = std_caller.Trace{clr.trace[0]}
if stdClr, ok := e.(std_error.Caller); ok {
clr.trace = append(clr.trace, stdClr.Caller().Trace()...)
}
return &E{
caller: clr,
err: e,
}
}
// Track updates the error stack with additional caller data.
func Track(e error) *E {
var stdE *E
if nil == e {
return nil
}
stdE, ok := e.(*E)
if !ok {
stdE = &E{
err: e,
}
if clr, ok := e.(std_error.Caller); ok {
stdE.caller = clr.Caller()
} else {
stdE.caller = NewCaller()
}
}
return &E{
caller: stdE.Caller(),
err: e,
prev: &E{
caller: NewCaller(),
err: fmt.Errorf("%s (tracked)", e),
prev: stdE.Unwrap(),
},
}
}
// Unwrap returns the previous error.
func Unwrap(err error) error {
if e, ok := err.(interface{ Unwrap() error }); ok {
return e.Unwrap()
}
return nil
}
// Wrap returns a new error that wraps the provided error.
func Wrap(e error, msg string, data ...interface{}) *E {
return WrapE(e, fmt.Errorf(msg, data...))
}
// WrapE returns a new error that wraps the provided error.
func WrapE(e, err error) *E {
return &E{
caller: NewCaller(),
err: err,
prev: e,
}
}