Skip to content

Commit f74b442

Browse files
committed
feat: errorlist helper
1 parent 03a2780 commit f74b442

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Diff for: helpers/errorlist/errorlist.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package errorlist
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type ErrorList []error
8+
9+
func (e ErrorList) Error() string {
10+
s := ""
11+
12+
for idx, err := range e {
13+
s = fmt.Sprintf("%d: %s\n", idx, err)
14+
}
15+
16+
return s
17+
}
18+
19+
// Append will append an error into [ErrorList]
20+
func (e ErrorList) Append(err error) ErrorList {
21+
return append(e, err)
22+
}
23+
24+
// New will create new instance of [ErrorList]
25+
//
26+
// use [nil] as parameter to create empty [ErrorList]
27+
//
28+
// use [Append] to add into [ErrorList]
29+
func New(err error) ErrorList {
30+
if err == nil {
31+
return make(ErrorList, 0)
32+
}
33+
34+
if errl, ok := err.(ErrorList); ok {
35+
return errl
36+
}
37+
38+
e := make(ErrorList, 0)
39+
e = append(e, err)
40+
return e
41+
}

0 commit comments

Comments
 (0)