/
error_group.go
48 lines (38 loc) · 933 Bytes
/
error_group.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
package comm
import (
"fmt"
)
type ErrorGroupT struct {
ellors []error
dumpStack bool
}
type ErrorGroup = *ErrorGroupT
func NewErrorGroup(dumpStack bool) ErrorGroup {
return &ErrorGroupT{
ellors: make([]error, 0, 8),
dumpStack: dumpStack,
}
}
func (me ErrorGroup) HasError() bool {
return me.AmountOfErrors() > 0
}
func (me ErrorGroup) AmountOfErrors() int {
return len(me.ellors)
}
func (me ErrorGroup) Error() string {
msgs := make([]string, 0, len(me.ellors)*2)
for i, err := range me.ellors {
if i > 0 && me.dumpStack {
msgs = append(msgs, "")
}
msg := fmt.Sprintf("error #%d - %s", i+1, err.Error())
msgs = append(msgs, msg)
}
return fmt.Sprintf("%d errors totally:\n%s", len(me.ellors), JoinedLines(msgs...))
}
func (me ErrorGroup) Add(err error) {
me.ellors = append(me.ellors, err)
}
func (me ErrorGroup) AddGroup(that ErrorGroup) {
me.ellors = append(me.ellors, that.ellors...)
}