-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmultierror.go
More file actions
108 lines (100 loc) · 2.34 KB
/
multierror.go
File metadata and controls
108 lines (100 loc) · 2.34 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
package xerrors
import (
"errors"
"strconv"
"strings"
)
// Append appends the provided errors to an existing error or list of
// errors. If `err` is not a [multiError], it will be converted into
// one. Nil errors are ignored. It does not record a stack trace.
//
// If the resulting error list is empty, nil is returned. If the
// resulting error list contains only one error, that error is
// returned instead of the list.
//
// The returned error is compatible with Go errors, supporting
// [errors.Is], [errors.As], and the Go 1.20 `Unwrap() []error`
// method.
//
// To create a chained error, use [New], [Newf], [Join], or
// [Joinf] instead.
func Append(err error, errs ...error) error {
var me multiError
if err != nil {
if mErr, ok := err.(multiError); ok {
me = mErr
} else {
me = multiError{err}
}
}
for _, e := range errs {
if e != nil {
me = append(me, e)
}
}
switch len(me) {
case 0:
return nil
case 1:
return me[0]
default:
return me
}
}
// multiError is a slice of errors that can be treated as a single
// error.
type multiError []error
// Error implements the [error] interface.
func (e multiError) Error() string {
var s strings.Builder
s.WriteString("[")
for n, err := range e {
s.WriteString(err.Error())
if n < len(e)-1 {
s.WriteString(", ")
}
}
s.WriteString("]")
return s.String()
}
// ErrorDetails returns additional details about the error for
// the [ErrorDetails] function.
func (e multiError) ErrorDetails() string {
if len(e) == 0 {
return ""
}
buf := &strings.Builder{}
for n, err := range e.Unwrap() {
buf.WriteString(strconv.Itoa(n + 1))
buf.WriteString(". ")
writeErr(buf, err)
}
return buf.String()
}
// Unwrap implements the Go 1.20 `Unwrap() []error` method, returning
// a slice containing all errors in the list.
func (e multiError) Unwrap() []error {
s := make([]error, len(e))
copy(s, e)
return s
}
// As implements the Go 1.13 `errors.As` method, allowing type
// assertions on all errors in the list.
func (e multiError) As(target any) bool {
for _, err := range e {
if errors.As(err, target) {
return true
}
}
return false
}
// Is implements the Go 1.13 `errors.Is` method, allowing
// comparisons with all errors in the list.
func (e multiError) Is(target error) bool {
for _, err := range e {
if errors.Is(err, target) {
return true
}
}
return false
}