-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
70 lines (66 loc) · 1.37 KB
/
errors.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
package iterutil
import "iter"
type (
bwrapper interface{ Unwrap() []error }
dwrapper interface{ Unwrap() error }
)
// AllErrors performs a preorder traversal of err and returns an iterator
// over its index-error pairs. For more context, see the [errors] package.
func AllErrors(err error) iter.Seq2[int, error] {
return func(yield func(int, error) bool) {
var i int
if !yield(i, err) {
return
}
i++
switch err := err.(type) {
case bwrapper:
for _, err := range err.Unwrap() {
for _, err := range AllErrors(err) {
if !yield(i, err) {
return
}
i++
}
}
case dwrapper:
for _, err := range AllErrors(err.Unwrap()) {
if !yield(i, err) {
return
}
i++
}
default:
return
}
}
}
// AllLeafErrors performs a preorder traversal of err and returns an iterator
// over its index-error leaf pairs. For more context, see the [errors] package.
func AllLeafErrors(err error) iter.Seq2[int, error] {
return func(yield func(int, error) bool) {
var i int
switch err := err.(type) {
case bwrapper:
for _, err := range err.Unwrap() {
for _, err := range AllLeafErrors(err) {
if !yield(i, err) {
return
}
i++
}
}
case dwrapper:
for _, err := range AllLeafErrors(err.Unwrap()) {
if !yield(i, err) {
return
}
i++
}
default:
if !yield(i, err) {
return
}
}
}
}