-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultierror.go
More file actions
62 lines (53 loc) · 1.18 KB
/
multierror.go
File metadata and controls
62 lines (53 loc) · 1.18 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
package execgroup
import "strings"
// MultiError contains several errors.
type MultiError []error
// Err returns the error, if it is not empty, nil otherwise.
func (e MultiError) Err() error {
if e.Empty() {
return nil
}
return e
}
// Errors returns the individual errors.
func (e MultiError) Errors() []error {
return e
}
// Empty returns true if there are no errors.
func (e MultiError) Empty() bool {
return len(e) == 0
}
// Len returns the number of errors.
func (e MultiError) Len() int {
return len(e)
}
// Append adds more errors.
func (e MultiError) Append(errs ...error) MultiError {
for _, err := range errs {
if me, ok := err.(MultiError); ok {
e = e.Append(me...)
} else if err != nil {
e = append(e, err)
}
}
return e
}
// Error concatenates the errors.
func (e MultiError) Error() string {
if len(e) == 0 {
return ""
}
if len(e) == 1 {
return e[0].Error()
}
var errors = make([]string, len(e))
for i, err := range e {
errors[i] = err.Error()
}
return "multiple (" + strings.Join(errors, "; ") + ")"
}
// NewMultiError returns a multi error from the provided.
func NewMultiError(errs ...error) MultiError {
var e MultiError
return e.Append(errs...)
}