-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherrors.go
More file actions
70 lines (58 loc) · 1.25 KB
/
errors.go
File metadata and controls
70 lines (58 loc) · 1.25 KB
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
64
65
66
67
68
69
70
package rulekit
import (
"errors"
"fmt"
"github.com/hashicorp/go-multierror"
"github.com/qpoint-io/rulekit/set"
)
type ErrMissingFields struct {
Fields set.Set[string]
}
func (e ErrMissingFields) Error() string {
return fmt.Sprintf("missing fields: %v", e.Fields.Items())
}
func coalesceErrs(errs ...error) error {
var (
multi = &multierror.Error{
ErrorFormat: func(errs []error) string {
switch len(errs) {
case 0:
return ""
case 1:
return errs[0].Error()
default:
return multierror.ListFormatFunc(errs)
}
},
}
// combine all ErrMissingFields errors
mf = set.NewSet[string]()
)
for _, err := range errs {
if e, ok := err.(*ErrMissingFields); ok {
mf.Merge(e.Fields)
continue
}
multi = multierror.Append(multi, err)
}
if mf.Len() > 0 {
multi = multierror.Append(multi, &ErrMissingFields{Fields: mf})
}
switch len(multi.Errors) {
case 0:
return nil
case 1:
return multi.Errors[0]
default:
return multi
}
}
var ErrInvalidOperation = errors.New("invalid operation")
type ErrInvalidFunctionArg struct {
Name string
Expected string
Got string
}
func (e *ErrInvalidFunctionArg) Error() string {
return fmt.Sprintf("arg %s: expected %s, got %s", e.Name, e.Expected, e.Got)
}