-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
55 lines (46 loc) · 966 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
52
53
54
55
package lerr
import "strings"
// Str provides a string error that can be set to a const making it good for
// sentinal errors.
type Str string
// Error fulfills error
func (err Str) Error() string {
return string(err)
}
// Many allows many errors to be collected.
type Many []error
func NewMany(errs ...error) Many {
var m Many
for _, err := range errs {
m = m.Add(err)
}
return m
}
func (m Many) First() error {
if len(m) > 0 {
return m[0]
}
return nil
}
// Error fulfills error
func (m Many) Error() string {
out := make([]string, 0, len(m))
for _, e := range m {
out = append(out, e.Error())
}
return strings.Join(out, "\n")
}
// Add an error to the collection. If err is nil, it will not be added
func (m Many) Add(err error) Many {
if err == nil {
return m
}
return append(m, err)
}
// Cast to error type. If Many contains no errors, it will return nil.
func (m Many) Cast() error {
if len(m) == 0 {
return nil
}
return m
}