-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
185 lines (168 loc) · 4.36 KB
/
errors.go
File metadata and controls
185 lines (168 loc) · 4.36 KB
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
package nstd
import (
"reflect"
"unique"
)
// Error returns an error that formats as the given text.
// Different from [errors.New] in the std library, two calls
// to Error return an identical error value if the texts are identical.
func Error(text string) error {
return errorString{unique.Make(text)}
}
// Another approach: type errorString string.
// But that results fat interface values.
type errorString struct {
s unique.Handle[string]
}
func (e errorString) Error() string {
return e.s.Value()
}
var errorType = reflect.TypeOf((*error)(nil)).Elem()
// TrackError reports whether any error in err's tree matches target.
// It behaves almost the same as [errors.Is], except that
//
// - TrackError(anIncomparbleErr, anIncomparbleErr) panics.
// - TrackError(&aComparableErr, aComparableErr) returns true.
// - It panics if the type of target is a pointer which base type's size is 0.
//
// See: https://github.com/golang/go/issues/74488
func TrackError(err, target error) bool {
if err == nil || target == nil {
return err == target
}
return trackError(err, target)
}
func trackError(err, target error) bool {
for {
if x, ok := err.(interface{ Is(error) bool }); ok {
if x.Is(target) {
return true
}
} else if err == target {
tt := reflect.TypeOf(target)
if tt.Kind() == reflect.Pointer && tt.Elem().Size() == 0 {
panic("target should not be a pointer pointing to a zero-size value")
}
return true
}
errValue := reflect.ValueOf(err)
errType := errValue.Type()
if errType.Kind() == reflect.Pointer && errType.Elem().Implements(errorType) && !errValue.IsNil() {
if trackError(errValue.Elem().Interface().(error), target) {
return true
}
}
switch x := err.(type) {
case interface{ Unwrap() error }:
err = x.Unwrap()
if err == nil {
return false
}
case interface{ Unwrap() []error }:
for _, err := range x.Unwrap() {
if trackError(err, target) {
return true
}
}
return false
default:
return false
}
}
}
// TrackErrorOf finds the first error in err's tree that matches ErrorType,
// and if one is found, returns a pointer to a copy of that error.
// Otherwise, it returns nil.
//
// Different from [errors.As],
//
// - If *ErrorType is also an error type, then TrackErrorOf
// doesn't distinguish ErrorType and *ErrorType.
// - If ErrorType is pointer type and its base type is also an error type,
// then TrackErrorOf doesn't distinguish ErrorType and its base type.
func TrackErrorOf[ErrorType error](err error, _ ...ErrorType) *ErrorType {
if err == nil {
return nil
}
var target = new(ErrorType)
var targetValue = reflect.ValueOf(target)
if n, value := trackErrorOf(err, target, targetValue); n != 0 {
if n != 1 {
targetValue.Elem().Set(value)
}
return target
}
return nil
}
func trackErrorOf(err error, target any, targetValue reflect.Value) (int, reflect.Value) {
var Type = targetValue.Type().Elem()
var info = targetInfo{
value: target,
reflectValue: targetValue,
reflectType: Type,
}
if trackOf(err, &info) {
return 1, reflect.Value{}
}
if Type.Kind() == reflect.Pointer {
Type = Type.Elem()
if Type.Implements(errorType) {
Value := reflect.New(Type)
info = targetInfo{
value: Value.Interface(),
reflectValue: Value,
reflectType: Type,
}
if trackOf(err, &info) {
return 2, Value
}
}
} else if Type.Kind() != reflect.Interface {
Type = reflect.PointerTo(Type)
Value := reflect.New(Type)
info = targetInfo{
value: Value.Interface(),
reflectValue: Value,
reflectType: Type,
}
if trackOf(err, &info) {
return 3, Value.Elem().Elem()
}
}
return 0, reflect.Value{}
}
type targetInfo struct {
value any
reflectValue reflect.Value
reflectType reflect.Type
}
func trackOf(err error, info *targetInfo) bool {
for {
if x, ok := err.(interface{ As(any) bool }); ok && x.As(info.value) {
return true
}
if reflect.TypeOf(err).AssignableTo(info.reflectType) {
info.reflectValue.Elem().Set(reflect.ValueOf(err))
return true
}
switch x := err.(type) {
case interface{ Unwrap() error }:
err = x.Unwrap()
if err == nil {
return false
}
case interface{ Unwrap() []error }:
for _, err := range x.Unwrap() {
if err == nil {
continue
}
if trackOf(err, info) {
return true
}
}
return false
default:
return false
}
}
}