-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
55 lines (45 loc) · 837 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 async
import (
"fmt"
"sync"
)
type (
Errors interface {
All() []error
ToError() error
IsEmpty() bool
}
errs struct {
mutex sync.Mutex
all []error
}
)
// Return []error.
func (ee *errs) All() []error {
return ee.all
}
// Return all errors as a single error.
func (ee *errs) ToError() error {
if ee.IsEmpty() {
return nil
}
return ee
}
// Return true if there are no errors.
func (ee *errs) IsEmpty() bool {
return len(ee.all) == 0
}
// Implement the error interface for errs.
func (ee *errs) Error() string {
errorStr := ""
for _, err := range ee.All() {
errorStr = fmt.Sprintf("%s\n%s", errorStr, err.Error())
}
return errorStr
}
// Safely append to the []error in errs struct.
func (ee *errs) append(err error) {
ee.mutex.Lock()
defer ee.mutex.Unlock()
ee.all = append(ee.all, err)
}