-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
error.go
85 lines (72 loc) · 1.88 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package tenant
import (
"fmt"
"strings"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
)
var (
// ErrNameisEmpty is when a name is empty
ErrNameisEmpty = &errors.Error{
Code: errors.EInvalid,
Msg: "name is empty",
}
// ErrIDNotUnique is used when attempting to create an org or bucket that already
// exists.
ErrIDNotUnique = &errors.Error{
Code: errors.EConflict,
Msg: "ID already exists",
}
// ErrFailureGeneratingID occurs ony when the random number generator
// cannot generate an ID in MaxIDGenerationN times.
ErrFailureGeneratingID = &errors.Error{
Code: errors.EInternal,
Msg: "unable to generate valid id",
}
// ErrOnboardingNotAllowed occurs when request to onboard comes in and we are not allowing this request
ErrOnboardingNotAllowed = &errors.Error{
Code: errors.EConflict,
Msg: "onboarding has already been completed",
}
ErrNotFound = &errors.Error{
Code: errors.ENotFound,
Msg: "not found",
}
)
type errSlice []error
func (e errSlice) Error() string {
l := len(e)
sb := strings.Builder{}
for i, err := range e {
if i > 0 {
sb.WriteRune('\n')
}
sb.WriteString(fmt.Sprintf("error %d/%d: %s", i+1, l, err.Error()))
}
return sb.String()
}
// AggregateError enables composing multiple errors.
// This is ideal in the case that you are applying functions with side effects to a slice of elements.
// E.g., deleting/updating a slice of resources.
type AggregateError struct {
errs errSlice
}
// NewAggregateError returns a new AggregateError.
func NewAggregateError() *AggregateError {
return &AggregateError{
errs: make([]error, 0),
}
}
// Add adds an error to the aggregate.
func (e *AggregateError) Add(err error) {
if err == nil {
return
}
e.errs = append(e.errs, err)
}
// Err returns a proper error from this aggregate error.
func (e *AggregateError) Err() error {
if len(e.errs) > 0 {
return e.errs
}
return nil
}