-
Notifications
You must be signed in to change notification settings - Fork 699
/
aggregate.go
48 lines (40 loc) · 887 Bytes
/
aggregate.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
package errortypes
import (
"bytes"
"strconv"
)
// AggregateError represents one or more errors.
type AggregateError struct {
Message string
Errors []error
}
// NewAggregateError builds a AggregateError struct.
func NewAggregateError(msg string, errs []error) AggregateError {
return AggregateError{
Message: msg,
Errors: errs,
}
}
// Error implements the standard error interface.
func (e AggregateError) Error() string {
if len(e.Errors) == 0 {
return ""
}
b := bytes.Buffer{}
b.WriteString(e.Message)
if len(e.Errors) == 1 {
b.WriteString(" (1 error):\n")
} else {
b.WriteString(" (")
b.WriteString(strconv.Itoa(len(e.Errors)))
b.WriteString(" errors):\n")
}
for i, err := range e.Errors {
b.WriteString(" ")
b.WriteString(strconv.Itoa(i + 1))
b.WriteString(": ")
b.WriteString(err.Error())
b.WriteString("\n")
}
return b.String()
}