-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy patherror.go
More file actions
264 lines (229 loc) · 8.5 KB
/
Copy patherror.go
File metadata and controls
264 lines (229 loc) · 8.5 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2026 The Bucketeer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package error
import (
"errors"
"fmt"
"maps"
"strconv"
"strings"
)
const (
AccountPackageName = "account"
FeaturePackageName = "feature"
SubscriptionPackageName = "subscription"
NotificationPackageName = "notification"
PushPackageName = "push"
TagPackageName = "tag"
EventCounterPackageName = "eventcounter"
EnvironmentPackageName = "environment"
AuditlogPackageName = "auditlog"
AutoopsPackageName = "autoops"
CoderefPackageName = "coderef"
TeamPackageName = "team"
ExperimentPackageName = "experiment"
AuthPackageName = "auth"
AIChatPackageName = "aichat"
invalidPrefix = "Invalid"
)
// ErrorType is also used as the message key.
type ErrorType string
const (
ErrorTypeNotFound ErrorType = "NotFoundError"
ErrorTypeAlreadyExists ErrorType = "AlreadyExistsError"
ErrorTypeUnauthenticated ErrorType = "UnauthenticatedError"
ErrorTypePermissionDenied ErrorType = "PermissionDeniedError"
ErrorTypeUnexpectedAffectedRows ErrorType = "UnexpectedAffectedRowsError"
ErrorTypeInternal ErrorType = "InternalServerError"
ErrorTypeFailedPrecondition ErrorType = "FailedPreconditionError"
ErrorTypeUnavailable ErrorType = "UnavailableError"
ErrorTypeAborted ErrorType = "AbortedError"
ErrorTypeInvalidArgUnknown ErrorType = "InvalidArgumentUnknownError"
ErrorTypeInvalidArgEmpty ErrorType = "InvalidArgumentEmptyError"
ErrorTypeInvalidArgNil ErrorType = "InvalidArgumentNilError"
ErrorTypeInvalidArgNotMatchFormat ErrorType = "InvalidArgumentNotMatchFormatError"
ErrorTypeInvalidArgDuplicated ErrorType = "InvalidArgumentDuplicatedError"
ErrorTypeExceededMax ErrorType = "ExceededMaxError"
ErrorTypeOutOfRange ErrorType = "OutOfRangeError"
ErrorTypeDifferentVariationsSize ErrorType = "DifferentVariationsSizeError"
// A variation cannot be deleted while something still references it.
ErrorTypeVariationInUseByOffVariation ErrorType = "VariationInUseByOffVariationError"
ErrorTypeVariationInUseByDefaultStrategy ErrorType = "VariationInUseByDefaultStrategyError"
ErrorTypeVariationInUseByTargetingRule ErrorType = "VariationInUseByTargetingRuleError"
ErrorTypeVariationInUseByIndividualTarget ErrorType = "VariationInUseByIndividualTargetingError"
ErrorTypeVariationInUseByPrerequisite ErrorType = "VariationInUseByPrerequisiteError"
ErrorTypeVariationInUseByFeatureFlagRule ErrorType = "VariationInUseByFeatureFlagRuleError"
)
type BktError struct {
packageName string
errorType ErrorType
message string
wrappedError error
field string // optional
embeddedKeyValues map[string]string
messageKeyOverride string
}
func (e *BktError) PackageName() string { return e.packageName }
func (e *BktError) ErrorType() ErrorType { return e.errorType }
func (e *BktError) MessageKey() string {
if e.messageKeyOverride != "" {
return e.messageKeyOverride
}
return string(e.errorType)
}
func (e *BktError) WithMessageKey(key string) *BktError {
e.messageKeyOverride = key
return e
}
func (e *BktError) EmbeddedKeyValues() map[string]string { return e.embeddedKeyValues }
func (e *BktError) Error() string {
msg := fmt.Sprintf("%s:%s", e.packageName, e.message)
if e.field != "" {
if strings.HasPrefix(string(e.errorType), invalidPrefix) {
msg += fmt.Sprintf("[%s:%s]", e.field, e.errorType)
} else {
msg += fmt.Sprintf(", %s", e.field)
}
}
if e.wrappedError != nil {
return fmt.Sprintf("%s: %v", msg, e.wrappedError)
}
return msg
}
func (e *BktError) Unwrap() error { return e.wrappedError }
func (e *BktError) Wrap(err error) {
e.wrappedError = errors.Join(e.wrappedError, err)
}
// AsVariationInUseError reports whether err says a variation is still referenced.
func AsVariationInUseError(err error) (*BktError, bool) {
var bktErr *BktError
if !errors.As(err, &bktErr) {
return nil, false
}
switch bktErr.ErrorType() {
case ErrorTypeVariationInUseByOffVariation,
ErrorTypeVariationInUseByDefaultStrategy,
ErrorTypeVariationInUseByTargetingRule,
ErrorTypeVariationInUseByIndividualTarget,
ErrorTypeVariationInUseByPrerequisite,
ErrorTypeVariationInUseByFeatureFlagRule:
return bktErr, true
}
return nil, false
}
func newBktError(pkg string, errorType ErrorType, message string) *BktError {
return &BktError{
packageName: pkg,
errorType: errorType,
message: message,
}
}
func newBktFieldError(pkg string, errorType ErrorType, message string, field string) *BktError {
return &BktError{
packageName: pkg,
errorType: errorType,
message: message,
field: field,
embeddedKeyValues: map[string]string{
"field": field,
},
}
}
func NewErrorNotFound(pkg string, message string, field string) *BktError {
return newBktFieldError(pkg, ErrorTypeNotFound, message, field)
}
func NewErrorAlreadyExists(pkg string, message string) *BktError {
return newBktError(pkg, ErrorTypeAlreadyExists, message)
}
func NewErrorUnauthenticated(pkg string, message string) *BktError {
return newBktError(pkg, ErrorTypeUnauthenticated, message)
}
func NewErrorPermissionDenied(pkg string, message string) *BktError {
return newBktError(pkg, ErrorTypePermissionDenied, message)
}
func NewErrorUnexpectedAffectedRows(pkg string, message string) *BktError {
return newBktError(pkg, ErrorTypeUnexpectedAffectedRows, message)
}
func NewErrorInternal(pkg string, message string) *BktError {
return newBktError(pkg, ErrorTypeInternal, message)
}
func NewErrorFailedPrecondition(pkg string, message string) *BktError {
return newBktError(pkg, ErrorTypeFailedPrecondition, message)
}
func NewErrorUnavailable(pkg string, message string) *BktError {
return newBktError(pkg, ErrorTypeUnavailable, message)
}
func NewErrorAborted(pkg string, message string) *BktError {
return newBktError(pkg, ErrorTypeAborted, message)
}
func NewErrorInvalidArgUnknown(pkg string, message string, field string) *BktError {
return newBktFieldError(pkg, ErrorTypeInvalidArgUnknown, message, field)
}
func NewErrorInvalidArgEmpty(pkg string, message string, field string) *BktError {
return newBktFieldError(pkg, ErrorTypeInvalidArgEmpty, message, field)
}
func NewErrorInvalidArgNil(pkg string, message string, field string) *BktError {
return newBktFieldError(pkg, ErrorTypeInvalidArgNil, message, field)
}
func NewErrorInvalidArgNotMatchFormat(pkg string, message string, field string) *BktError {
return newBktFieldError(pkg, ErrorTypeInvalidArgNotMatchFormat, message, field)
}
func NewErrorInvalidArgDuplicated(pkg string, message string, field string) *BktError {
return newBktFieldError(pkg, ErrorTypeInvalidArgDuplicated, message, field)
}
func NewErrorExceededMax(pkg string, message string, field string, max int) *BktError {
return &BktError{
packageName: pkg,
errorType: ErrorTypeExceededMax,
message: message,
field: field,
embeddedKeyValues: map[string]string{
"field": field,
"max": strconv.Itoa(max),
},
}
}
func NewErrorOutOfRange(pkg string, message string, field string, min int, max int) *BktError {
return &BktError{
packageName: pkg,
errorType: ErrorTypeOutOfRange,
message: message,
field: field,
embeddedKeyValues: map[string]string{
"field": field,
"min": strconv.Itoa(min),
"max": strconv.Itoa(max),
},
}
}
func NewErrorDifferentVariationsSize(pkg string, message string) *BktError {
return newBktError(pkg, ErrorTypeDifferentVariationsSize, message)
}
// NewErrorVariationInUse creates an error naming the reference that keeps a
// variation from being deleted. keyValues may be nil.
func NewErrorVariationInUse(
pkg string,
errorType ErrorType,
message string,
keyValues map[string]string,
) *BktError {
embedded := make(map[string]string, len(keyValues))
maps.Copy(embedded, keyValues)
return &BktError{
packageName: pkg,
errorType: errorType,
message: message,
embeddedKeyValues: embedded,
}
}