-
Notifications
You must be signed in to change notification settings - Fork 18
/
err_multiple.go
59 lines (49 loc) · 1.12 KB
/
err_multiple.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
package floc
import (
"strconv"
"strings"
)
// ErrMultiple contains multiple errors.
type ErrMultiple struct {
list []error
}
// NewErrMultiple construct error instance from the list of errors given.
func NewErrMultiple(errs ...error) ErrMultiple {
return ErrMultiple{
list: errs,
}
}
// Top returns the top most error from the contained list.
func (err ErrMultiple) Top() error {
if len(err.list) > 0 {
return err.list[0]
}
return nil
}
// List returns the list of errors contained.
func (err ErrMultiple) List() []error {
return err.list
}
// Len returns the count of errors contained.
func (err ErrMultiple) Len() int {
return len(err.list)
}
func (err ErrMultiple) Error() string {
// Return the first error if only one error is contained.
if len(err.list) == 1 {
return err.list[0].Error()
}
// Build the string from all errors contained.
sb := strings.Builder{}
sb.WriteString(strconv.Itoa(len(err.list)))
sb.WriteString(" errors: ")
for i, err := range err.list {
if i != 0 {
sb.WriteString(", ")
}
sb.WriteByte('"')
sb.WriteString(err.Error())
sb.WriteByte('"')
}
return sb.String()
}