-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
52 lines (43 loc) · 1.24 KB
/
error.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
package envparse
import "fmt"
// ErrorList implements `error` interface with ability to combine multiple errors togethers
type ErrorList struct {
errors []error
}
// FullError will return a new error made using `(*ErrorList).FullError()` or will return original `error` otherwise
func FullError(err error) error {
if errorList, ok := err.(*ErrorList); ok {
return fmt.Errorf(errorList.FullError())
} else {
return err
}
}
func newErrorList() *ErrorList {
return &ErrorList{errors: make([]error, 0)}
}
// Error is needed to implement `error` interface
func (e *ErrorList) Error() string {
return fmt.Sprintf("%d errors happened during parsing environment variables", len(e.errors))
}
// FullError combined all errors together in single message
func (e *ErrorList) FullError() string {
msg := e.Error()
if len(e.errors) > 0 {
msg = fmt.Sprintf("%s:", msg)
}
for _, err := range e.errors {
msg += fmt.Sprintf("\n\t- %s", err.Error())
}
return msg
}
// IsEmpty reports if *ErrorList is empty (0 appended errors)
func (e *ErrorList) IsEmpty() bool {
if e.errors == nil || len(e.errors) == 0 {
return true
}
return false
}
// Append adds a new error to the stack
func (e *ErrorList) Append(err error) {
e.errors = append(e.errors, err)
}