-
Notifications
You must be signed in to change notification settings - Fork 474
/
errors.go
51 lines (41 loc) · 931 Bytes
/
errors.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
package util
import (
"errors"
"fmt"
"strings"
)
// Errors is a struct implementing error interface, allowing to accumulate several errors as a flat list
type Errors struct {
errors []error
}
func (in *Errors) Add(err error) {
in.errors = append(in.errors, err)
}
func (in *Errors) AddString(str string) {
in.errors = append(in.errors, errors.New(str))
}
func (in *Errors) Count() int {
return len(in.errors)
}
func (in *Errors) IsEmpty() bool {
return len(in.errors) == 0
}
func (in *Errors) Merge(others *Errors) {
in.errors = append(in.errors, others.errors...)
}
func (in *Errors) Strings() []string {
var str []string
for _, err := range in.errors {
str = append(str, err.Error())
}
return str
}
func (in *Errors) Error() string {
return fmt.Sprintf("%d errors:\n%s", in.Count(), strings.Join(in.Strings(), "\n"))
}
func (in *Errors) OrNil() *Errors {
if in.IsEmpty() {
return nil
}
return in
}