-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultipleerror.go
More file actions
57 lines (50 loc) · 1.12 KB
/
multipleerror.go
File metadata and controls
57 lines (50 loc) · 1.12 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
package socketcan
import (
"strings"
)
// MultipleError implements the error interface and may contain multiple errors
type MultipleError struct {
errors []error
}
// Add adds the given error to the contained errors
func (e *MultipleError) Add(err error) {
if e.errors == nil {
e.errors = make([]error, 0)
}
e.errors = append(e.errors, err)
}
// Err returns, dependent upon the count of contained errors, nil, the one
// contained error or the MultipleError itself.
// This should be returned as error to the calling function.
func (e *MultipleError) Err() (err error) {
switch len(e.errors) {
case 0:
err = nil
case 1:
err = e.errors[0]
default:
err = e
}
return
}
// Errors returns the contained errors
func (e *MultipleError) Errors() (errs []error) {
errs = e.errors
return
}
// Error implements the error interface
func (e *MultipleError) Error() (s string) {
switch len(e.errors) {
case 0:
s = ""
case 1:
s = e.errors[0].Error()
default:
ss := make([]string, 0)
for _, err := range e.errors {
ss = append(ss, err.Error())
}
s = "multiple errors occurred: " + strings.Join(ss, ", ")
}
return
}