-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
55 lines (47 loc) · 1.01 KB
/
collector.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 errors
import "fmt"
// Collector handle multiple errors
type Collector struct {
Errors []*Error
}
// NewCollector returns a new Collector
func NewCollector() *Collector {
return &Collector{
Errors: []*Error{},
}
}
// Push adds Error to the Collector
func (c *Collector) Push(e *Error) {
c.Errors = append(c.Errors, e)
}
// IsFatal returns true if one error is a fatal one
func (c *Collector) IsFatal() bool {
for _, e := range c.Errors {
if e.IsFatal() {
return true
}
}
return false
}
// HasErrors returns true if there are errors in the
// collector
func (c *Collector) HasErrors() bool {
if len(c.Errors) == 0 {
return false
}
return true
}
// Error implements error golang interface
func (c *Collector) Error() string {
switch len(c.Errors) {
case 0:
return fmt.Sprintf("No error")
case 1:
return c.Errors[0].ErrorWithCtx()
}
str := fmt.Sprintf("Got %d errors:\n", len(c.Errors))
for _, e := range c.Errors {
str = fmt.Sprintf("%s%s\n", str, e.ErrorWithCtx())
}
return str
}