-
Notifications
You must be signed in to change notification settings - Fork 81
/
errortypes.go
473 lines (417 loc) · 14.3 KB
/
errortypes.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package errors
import (
"errors"
stderror "errors"
"fmt"
"strings"
)
// a ConstError is a prototype for a certain type of error
type ConstError string
// ConstError implements error
func (e ConstError) Error() string {
return string(e)
}
// Different types of errors
const (
// Timeout represents an error on timeout.
Timeout = ConstError("timeout")
// NotFound represents an error when something has not been found.
NotFound = ConstError("not found")
// UserNotFound represents an error when a non-existent user is looked up.
UserNotFound = ConstError("user not found")
// Unauthorized represents an error when an operation is unauthorized.
Unauthorized = ConstError("unauthorized")
// NotImplemented represents an error when something is not
// implemented.
NotImplemented = ConstError("not implemented")
// AlreadyExists represents and error when something already exists.
AlreadyExists = ConstError("already exists")
// NotSupported represents an error when something is not supported.
NotSupported = ConstError("not supported")
// NotValid represents an error when something is not valid.
NotValid = ConstError("not valid")
// NotProvisioned represents an error when something is not yet provisioned.
NotProvisioned = ConstError("not provisioned")
// NotAssigned represents an error when something is not yet assigned to
// something else.
NotAssigned = ConstError("not assigned")
// BadRequest represents an error when a request has bad parameters.
BadRequest = ConstError("bad request")
// MethodNotAllowed represents an error when an HTTP request
// is made with an inappropriate method.
MethodNotAllowed = ConstError("method not allowed")
// Forbidden represents an error when a request cannot be completed because of
// missing privileges.
Forbidden = ConstError("forbidden")
// QuotaLimitExceeded is emitted when an action failed due to a quota limit check.
QuotaLimitExceeded = ConstError("quota limit exceeded")
// NotYetAvailable is the error returned when a resource is not yet available
// but it might be in the future.
NotYetAvailable = ConstError("not yet available")
)
// errWithType is an Err bundled with its error type (a ConstError)
type errWithType struct {
error
errType ConstError
}
// Is compares `target` with e's error type
func (e *errWithType) Is(target error) bool {
if &e.errType == nil {
return false
}
return target == e.errType
}
// Unwrap an errWithType gives the underlying Err
func (e *errWithType) Unwrap() error {
return e.error
}
func wrapErrorWithMsg(err error, msg string) error {
if err == nil {
return stderror.New(msg)
}
if msg == "" {
return err
}
return fmt.Errorf("%s: %w", msg, err)
}
func makeWrappedConstError(err error, format string, args ...interface{}) error {
separator := " "
if err.Error() == "" || errors.Is(err, &fmtNoop{}) {
separator = ""
}
return fmt.Errorf(strings.Join([]string{format, "%w"}, separator), append(args, err)...)
}
// WithType is responsible for annotating an already existing error so that it
// also satisfies that of a ConstError. The resultant error returned should
// satisfy Is(err, errType). If err is nil then a nil error will also be returned.
//
// Now with Go's Is, As and Unwrap support it no longer makes sense to Wrap()
// 2 errors as both of those errors could be chains of errors in their own right.
// WithType aims to solve some of the usefulness of Wrap with the ability to
// make a pre-existing error also satisfy a ConstError type.
func WithType(err error, errType ConstError) error {
if err == nil {
return nil
}
return &errWithType{
error: err,
errType: errType,
}
}
// Timeoutf returns an error which satisfies Is(err, Timeout) and the Locationer
// interface.
func Timeoutf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Timeout, format, args...),
1,
)
}
// NewTimeout returns an error which wraps err and satisfies Is(err, Timeout)
// and the Locationer interface.
func NewTimeout(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: Timeout,
}
}
// Deprecated: IsTimeout reports whether err is a Timeout error. Use
// Is(err, Timeout).
func IsTimeout(err error) bool {
return Is(err, Timeout)
}
// NotFoundf returns an error which satisfies Is(err, NotFound) and the
// Locationer interface.
func NotFoundf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(NotFound, format, args...),
1,
)
}
// NewNotFound returns an error which wraps err and satisfies Is(err, NotFound)
// and the Locationer interface.
func NewNotFound(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: NotFound,
}
}
// Deprecated: IsNotFound reports whether err is a NotFound error. Use
// Is(err, NotFound).
func IsNotFound(err error) bool {
return Is(err, NotFound)
}
// UserNotFoundf returns an error which satisfies Is(err, UserNotFound) and the
// Locationer interface.
func UserNotFoundf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(UserNotFound, format, args...),
1,
)
}
// NewUserNotFound returns an error which wraps err and satisfies
// Is(err, UserNotFound) and the Locationer interface.
func NewUserNotFound(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: UserNotFound,
}
}
// Deprecated: IsUserNotFound reports whether err is a UserNotFound error. Use
// Is(err, UserNotFound).
func IsUserNotFound(err error) bool {
return Is(err, UserNotFound)
}
// Unauthorizedf returns an error that satisfies Is(err, Unauthorized) and
// the Locationer interface.
func Unauthorizedf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Hide(Unauthorized), format, args...),
1,
)
}
// NewUnauthorized returns an error which wraps err and satisfies
// Is(err, Unathorized) and the Locationer interface.
func NewUnauthorized(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: Unauthorized,
}
}
// Deprecated: IsUnauthorized reports whether err is a Unauthorized error. Use
// Is(err, Unauthorized).
func IsUnauthorized(err error) bool {
return Is(err, Unauthorized)
}
// NotImplementedf returns an error which satisfies Is(err, NotImplemented) and
// the Locationer interface.
func NotImplementedf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(NotImplemented, format, args...),
1,
)
}
// NewNotImplemented returns an error which wraps err and satisfies
// Is(err, NotImplemented) and the Locationer interface.
func NewNotImplemented(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: NotImplemented,
}
}
// Deprecated: IsNotImplemented reports whether err is a NotImplemented error.
// Use Is(err, NotImplemented).
func IsNotImplemented(err error) bool {
return Is(err, NotImplemented)
}
// AlreadyExistsf returns an error which satisfies Is(err, AlreadyExists) and
// the Locationer interface.
func AlreadyExistsf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(AlreadyExists, format, args...),
1,
)
}
// NewAlreadyExists returns an error which wraps err and satisfies
// Is(err, AlreadyExists) and the Locationer interface.
func NewAlreadyExists(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: AlreadyExists,
}
}
// Deprecated: IsAlreadyExists reports whether the err is a AlreadyExists
// error. Use Is(err, AlreadyExists).
func IsAlreadyExists(err error) bool {
return Is(err, AlreadyExists)
}
// NotSupportedf returns an error which satisfies Is(err, NotSupported) and the
// Locationer interface.
func NotSupportedf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(NotSupported, format, args...),
1,
)
}
// NewNotSupported returns an error which satisfies Is(err, NotSupported) and
// the Locationer interface.
func NewNotSupported(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: NotSupported,
}
}
// Deprecated: IsNotSupported reports whether err is a NotSupported error. Use
// Is(err, NotSupported).
func IsNotSupported(err error) bool {
return Is(err, NotSupported)
}
// NotValidf returns an error which satisfies Is(err, NotValid) and the
// Locationer interface.
func NotValidf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(NotValid, format, args...),
1,
)
}
// NewNotValid returns an error which wraps err and satisfies Is(err, NotValid)
// and the Locationer interface.
func NewNotValid(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: NotValid,
}
}
// Deprecated: IsNotValid reports whether err is a NotValid error. Use
// Is(err, NotValid).
func IsNotValid(err error) bool {
return Is(err, NotValid)
}
// NotProvisionedf returns an error which satisfies Is(err, NotProvisioned) and
// the Locationer interface.
func NotProvisionedf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(NotProvisioned, format, args...),
1,
)
}
// NewNotProvisioned returns an error which wraps err and satisfies
// Is(err, NotProvisioned) and the Locationer interface.
func NewNotProvisioned(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: NotProvisioned,
}
}
// Deprecated: IsNotProvisioned reports whether err is a NotProvisioned error.
// Use Is(err, NotProvisioned).
func IsNotProvisioned(err error) bool {
return Is(err, NotProvisioned)
}
// NotAssignedf returns an error which satisfies Is(err, NotAssigned) and the
// Locationer interface.
func NotAssignedf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(NotAssigned, format, args...),
1,
)
}
// NewNotAssigned returns an error which wraps err and satisfies
// Is(err, NotAssigned) and the Locationer interface.
func NewNotAssigned(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: NotAssigned,
}
}
// Deprecated: IsNotAssigned reports whether err is a NotAssigned error.
// Use Is(err, NotAssigned)
func IsNotAssigned(err error) bool {
return Is(err, NotAssigned)
}
// BadRequestf returns an error which satisfies Is(err, BadRequest) and the
// Locationer interface.
func BadRequestf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Hide(BadRequest), format, args...),
1,
)
}
// NewBadRequest returns an error which wraps err and satisfies
// Is(err, BadRequest) and the Locationer interface.
func NewBadRequest(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: BadRequest,
}
}
// Deprecated: IsBadRequest reports whether err is a BadRequest error.
// Use Is(err, BadRequest)
func IsBadRequest(err error) bool {
return Is(err, BadRequest)
}
// MethodNotAllowedf returns an error which satisfies Is(err, MethodNotAllowed)
// and the Locationer interface.
func MethodNotAllowedf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Hide(MethodNotAllowed), format, args...),
1,
)
}
// NewMethodNotAllowed returns an error which wraps err and satisfies
// Is(err, MethodNotAllowed) and the Locationer interface.
func NewMethodNotAllowed(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: MethodNotAllowed,
}
}
// Deprecated: IsMethodNotAllowed reports whether err is a MethodNotAllowed
// error. Use Is(err, MethodNotAllowed)
func IsMethodNotAllowed(err error) bool {
return Is(err, MethodNotAllowed)
}
// Forbiddenf returns an error which satistifes Is(err, Forbidden) and the
// Locationer interface.
func Forbiddenf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Hide(Forbidden), format, args...),
1,
)
}
// NewForbidden returns an error which wraps err and satisfies
// Is(err, Forbidden) and the Locationer interface.
func NewForbidden(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: Forbidden,
}
}
// Deprecated: IsForbidden reports whether err is a Forbidden error. Use
// Is(err, Forbidden).
func IsForbidden(err error) bool {
return Is(err, Forbidden)
}
// QuotaLimitExceededf returns an error which satisfies
// Is(err, QuotaLimitExceeded) and the Locationer interface.
func QuotaLimitExceededf(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Hide(QuotaLimitExceeded), format, args...),
1,
)
}
// NewQuotaLimitExceeded returns an error which wraps err and satisfies
// Is(err, QuotaLimitExceeded) and the Locationer interface.
func NewQuotaLimitExceeded(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: QuotaLimitExceeded,
}
}
// Deprecated: IsQuotaLimitExceeded reports whether err is a QuoteLimitExceeded
// err. Use Is(err, QuotaLimitExceeded).
func IsQuotaLimitExceeded(err error) bool {
return Is(err, QuotaLimitExceeded)
}
// NotYetAvailablef returns an error which satisfies Is(err, NotYetAvailable)
// and the Locationer interface.
func NotYetAvailablef(format string, args ...interface{}) error {
return newLocationError(
makeWrappedConstError(Hide(NotYetAvailable), format, args...),
1,
)
}
// NewNotYetAvailable returns an error which wraps err and satisfies
// Is(err, NotYetAvailable) and the Locationer interface.
func NewNotYetAvailable(err error, msg string) error {
return &errWithType{
error: newLocationError(wrapErrorWithMsg(err, msg), 1),
errType: NotYetAvailable,
}
}
// Deprecated: IsNotYetAvailable reports whether err is a NotYetAvailable err.
// Use Is(err, NotYetAvailable)
func IsNotYetAvailable(err error) bool {
return Is(err, NotYetAvailable)
}