-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
63 lines (52 loc) · 1.17 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
53
54
55
56
57
58
59
60
61
62
63
package fender
import (
"errors"
"strings"
"github.com/foomo/fender/config"
"github.com/foomo/fender/fend"
"github.com/foomo/fender/rule"
)
type Error struct {
FendErrs []*fend.Error
}
func NewError(fendErrs ...*fend.Error) *Error {
return &Error{
FendErrs: fendErrs,
}
}
func NewFendError(name string, ruleErrs ...*rule.Error) *Error {
return NewError(fend.NewError(name, ruleErrs...))
}
func NewFendRuleError(name string, ruleName rule.Name, ruleMeta ...string) *Error {
return NewFendError(name, rule.NewError(ruleName, ruleMeta...))
}
func (e *Error) Error() string {
causes := make([]string, len(e.FendErrs))
for i, cause := range e.FendErrs {
causes[i] = cause.Error()
}
return strings.Join(causes, config.DelimiterFend)
}
func (e *Error) First() error {
if errs := e.Errors(); len(errs) > 0 {
return errs[0]
}
return nil
}
func (e *Error) Errors() []error {
causes := make([]error, len(e.FendErrs))
for i, fendErr := range e.FendErrs {
causes[i] = fendErr
}
return causes
}
// func (e *Error) Unwrap() error {
// return e.cause
// }
func AsError(err error) *Error {
var fendErr *Error
if errors.As(err, &fendErr) {
return fendErr
}
return nil
}