-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherror.go
97 lines (83 loc) · 1.88 KB
/
error.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
package errors
import (
"reflect"
std_caller "github.com/bdlm/std/v2/caller"
std_error "github.com/bdlm/std/v2/errors"
)
// E is a github.com/bdlm/std.Error interface implementation and simply wraps
// the exported package methods as a convenience.
type E struct {
caller std_caller.Caller
err error
prev error
}
// Caller implements std_error.Caller.
func (e *E) Caller() std_caller.Caller {
if nil == e {
return nil
}
return e.caller
}
// Error implements std_error.Error.
func (e *E) Error() string {
if nil == e.err {
return ""
}
return e.err.Error()
}
// Is implements std_error.Error.
func (e *E) Is(test error) bool {
if nil == test {
return false
}
isComparable := reflect.TypeOf(e).Comparable() && reflect.TypeOf(test).Comparable()
if isComparable && e == test {
return true
}
isComparable = reflect.TypeOf(e.err).Comparable() && reflect.TypeOf(test).Comparable()
if isComparable && e.err == test {
return true
}
if testE, ok := test.(*E); ok {
isComparable = reflect.TypeOf(e).Comparable() && reflect.TypeOf(testE).Comparable()
if isComparable && e == testE {
return true
}
isComparable = reflect.TypeOf(e.err).Comparable() && reflect.TypeOf(testE.err).Comparable()
if isComparable && e.err == testE.err {
return true
}
}
if err := e.Unwrap(); nil != err {
if err, ok := err.(interface{ Is(error) bool }); ok {
return err.Is(test)
}
}
return false
}
// Unwrap implements std_error.Wrapper.
func (e *E) Unwrap() error {
if nil == e {
return nil
}
if nil == e.prev {
return nil
}
if prev, ok := e.prev.(std_error.Error); ok {
return prev
}
return &E{
err: e.prev,
}
}
// list will convert the error stack into a simple array.
func list(e error) []error {
ret := []error{}
if nil != e {
if std, ok := e.(std_error.Wrapper); ok {
ret = append(ret, e)
ret = append(ret, list(std.Unwrap())...)
}
}
return ret
}