-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errutil.go
147 lines (139 loc) · 4.32 KB
/
errutil.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
/*
Package errutil provides utility functions for working with errors.
The advent of [errors.As] in the standard library predates that of parametric
polymorphism (generics) in the language.
As a result, [errors.As] is not as ergonomic, type-safe, or efficient as it
ideally could be.
Functions [As] and [Find] are inspired by several unaccepted proposals
(see issues [51945], [56949], and [64771]) and aim to address those limitations.
In most cases, [As] can be used as a drop-in replacement for [errors.As].
[Find] is a more efficient and arguably more ergonomic alternative to [As].
Incidentally, [the error-inspection draft design proposal] suggests that [errors.As]
would have been very similar to [Find] if the Go team had cracked
the parametric-polymorphism nut in time for [errors.As]'s inception in the
standard library.
In many cases, a call to [errors.As] can advantageously be refactored to a call
to [Find].
[51945]: https://github.com/golang/go/issues/51945
[56949]: https://github.com/golang/go/issues/56949
[64771]: https://github.com/golang/go/issues/64771
[the error-inspection draft design proposal]: https://go.googlesource.com/proposal/+/master/design/go2draft-error-inspection.md#the-is-and-as-functions
*/
package errutil
// As finds the first error in err's tree that matches target,
// and if one is found, sets target to that error value and returns true.
// Otherwise, it returns false.
//
// The tree consists of err itself, followed by the errors obtained by repeatedly
// calling its Unwrap() error or Unwrap() []error method. When err wraps multiple
// errors, As examines err followed by a depth-first traversal of its children.
//
// An error matches target if the error's concrete value is assignable to the value
// pointed to by target
// or if the error has a method As(any) bool such that As(target) returns true.
// In the latter case, the As method is responsible for setting target.
//
// An error type might provide an As method so it can be treated as if it were a
// different error type.
//
// As panics if err is not nil and target is nil.
func As[T error](err error, target *T) bool {
if err == nil {
return false
}
if target == nil {
panic("errutil: target cannot be nil")
}
return as(err, target)
}
func as[T error](err error, target *T) bool {
for {
if x, ok := err.(T); ok {
*target = x
return true
}
if x, ok := err.(interface{ As(any) bool }); ok && x.As(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 err == nil {
continue
}
if as(err, target) {
return true
}
}
return false
default:
return false
}
}
}
// Find finds the first error in err's tree that matches type T,
// and if so, returns the corresponding value and true.
// Otherwise, it returns the zero value and false.
//
// The tree consists of err itself, followed by the errors obtained by repeatedly
// calling its Unwrap() error or Unwrap() []error method. When err wraps multiple
// errors, Find examines err followed by a depth-first traversal of its children.
//
// An error matches type T if type-asserting it to T succeeds,
// or if the error has a method As(any) bool such that As(target),
// where target is any non-nil value of type *T, returns true.
// In the latter case, the As method is responsible for setting target.
//
// An error type might provide an As method so it can be treated as if it were a
// different error type.
func Find[T error](err error) (T, bool) {
if err == nil {
var zero T
return zero, false
}
var ptr *T
return find[T](err, &ptr)
}
func find[T error](err error, ptr2 **T) (T, bool) {
for {
x, ok := err.(T)
if ok {
return x, true
}
if x, ok := err.(interface{ As(any) bool }); ok {
if *ptr2 == nil {
*ptr2 = new(T)
}
if x.As(*ptr2) {
return **ptr2, true
}
}
switch x := err.(type) {
case interface{ Unwrap() error }:
err = x.Unwrap()
if err == nil {
var zero T
return zero, false
}
case interface{ Unwrap() []error }:
for _, err := range x.Unwrap() {
if err == nil {
continue
}
if x, ok := find[T](err, ptr2); ok {
return x, true
}
}
var zero T
return zero, false
default:
var zero T
return zero, false
}
}
}