forked from lovoo/goka
/
errors.go
65 lines (56 loc) · 1.03 KB
/
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
56
57
58
59
60
61
62
63
64
65
package multierr
import (
"fmt"
"sync"
)
// Errors represent a list of errors triggered during the execution of a goka view/processor.
// Normally, the first error leads to stopping the processor/view, but during shutdown, more errors
// might occur.
type Errors struct {
errs []error
m sync.Mutex
}
func (e *Errors) Collect(err error) *Errors {
if err == nil {
return e
}
e.m.Lock()
e.errs = append(e.errs, err)
e.m.Unlock()
return e
}
func (e *Errors) Merge(o *Errors) *Errors {
if o == nil {
return e
}
// lock base
e.m.Lock()
defer e.m.Unlock()
// lock other
o.m.Lock()
defer o.m.Unlock()
e.errs = append(e.errs, o.errs...)
return e
}
func (e *Errors) HasErrors() bool {
return len(e.errs) > 0
}
func (e *Errors) Error() string {
if !e.HasErrors() {
return ""
}
if len(e.errs) == 1 {
return e.errs[0].Error()
}
str := "Errors:\n"
for _, err := range e.errs {
str += fmt.Sprintf("\t* %s\n", err.Error())
}
return str
}
func (e *Errors) NilOrError() error {
if e.HasErrors() {
return e
}
return nil
}