forked from elves/elvish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
45 lines (40 loc) · 747 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
package util
import "bytes"
// Errors pack multiple errors into one error.
type Errors struct {
Errors []error
}
func (es *Errors) Error() string {
switch len(es.Errors) {
case 0:
return "no error"
case 1:
return es.Errors[0].Error()
default:
var buf bytes.Buffer
buf.WriteString("multiple errors: ")
for i, e := range es.Errors {
if i > 0 {
buf.WriteString("; ")
}
buf.WriteString(e.Error())
}
return buf.String()
}
}
func (es *Errors) Append(e error) {
es.Errors = append(es.Errors, e)
}
func CatError(err error, more error) error {
if err == nil {
return more
}
if more == nil {
return err
}
if es, ok := err.(*Errors); ok {
es.Append(more)
return es
}
return &Errors{[]error{err, more}}
}