-
Notifications
You must be signed in to change notification settings - Fork 18
/
errors.go
641 lines (537 loc) · 15.8 KB
/
errors.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
Copyright 2015 Gravitational, Inc.
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 trace
import (
"crypto/x509"
"errors"
"fmt"
"io"
"net"
"net/url"
"os"
"github.com/gravitational/trace/internal"
)
// NotFound returns new instance of not found error
func NotFound(message string, args ...interface{}) Error {
return newTrace(&NotFoundError{
Message: fmt.Sprintf(message, args...),
})
}
// NotFoundError indicates that object has not been found
type NotFoundError struct {
Message string `json:"message"`
}
// IsNotFoundError returns true to indicate that is NotFoundError
func (e *NotFoundError) IsNotFoundError() bool {
return true
}
// Error returns log friendly description of an error
func (e *NotFoundError) Error() string {
if e.Message != "" {
return e.Message
}
return "object not found"
}
// OrigError returns original error (in this case this is the error itself)
func (e *NotFoundError) OrigError() error {
return e
}
// Is provides an equivalency check for NotFoundError to be used with errors.Is
func (e *NotFoundError) Is(target error) bool {
if os.IsNotExist(target) {
return true
}
err, ok := Unwrap(target).(*NotFoundError)
if !ok {
return false
}
return err.Message == e.Message
}
// IsNotFound returns true if `e` contains a [NotFoundError] in its chain.
func IsNotFound(err error) bool {
return internal.TraverseErr(err, func(err error) (ok bool) {
if os.IsNotExist(err) {
return true
}
_, ok = err.(*NotFoundError)
return ok
})
}
// AlreadyExists returns a new instance of AlreadyExists error
func AlreadyExists(message string, args ...interface{}) Error {
return newTrace(&AlreadyExistsError{
Message: fmt.Sprintf(message, args...),
})
}
// AlreadyExistsError indicates that there's a duplicate object that already
// exists in the storage/system
type AlreadyExistsError struct {
Message string `json:"message"`
}
// Error returns log friendly description of an error
func (e *AlreadyExistsError) Error() string {
if e.Message != "" {
return e.Message
}
return "object already exists"
}
// IsAlreadyExistsError indicates that this error of the AlreadyExistsError type
func (AlreadyExistsError) IsAlreadyExistsError() bool {
return true
}
// OrigError returns original error (in this case this is the error itself)
func (e *AlreadyExistsError) OrigError() error {
return e
}
// Is provides an equivalency check for AlreadyExistsError to be used with errors.Is
func (e *AlreadyExistsError) Is(target error) bool {
err, ok := Unwrap(target).(*AlreadyExistsError)
if !ok {
return false
}
return err.Message == e.Message
}
// IsAlreadyExists returns true if `e` contains an [AlreadyExistsError] in its
// chain.
func IsAlreadyExists(e error) bool {
other := &AlreadyExistsError{}
return errors.As(e, &other)
}
// BadParameter returns a new instance of BadParameterError
func BadParameter(message string, args ...interface{}) Error {
return newTrace(&BadParameterError{
Message: fmt.Sprintf(message, args...),
})
}
// BadParameterError indicates that something is wrong with passed
// parameter to API method
type BadParameterError struct {
Message string `json:"message"`
}
// Error returns log friendly description of an error
func (b *BadParameterError) Error() string {
return b.Message
}
// OrigError returns original error (in this case this is the error itself)
func (b *BadParameterError) OrigError() error {
return b
}
// IsBadParameterError indicates that this error is of BadParameterError type
func (b *BadParameterError) IsBadParameterError() bool {
return true
}
// Is provides an equivalency check for BadParameterError to be used with errors.Is
func (b *BadParameterError) Is(target error) bool {
err, ok := Unwrap(target).(*BadParameterError)
if !ok {
return false
}
return err.Message == b.Message
}
// IsBadParameter returns true if `e` contains a [BadParameterError] in its
// chain.
func IsBadParameter(e error) bool {
other := &BadParameterError{}
return errors.As(e, &other)
}
// NotImplemented returns a new instance of NotImplementedError
func NotImplemented(message string, args ...interface{}) Error {
return newTrace(&NotImplementedError{
Message: fmt.Sprintf(message, args...),
})
}
// NotImplementedError defines an error condition to describe the result
// of a call to an unimplemented API
type NotImplementedError struct {
Message string `json:"message"`
}
// Error returns log friendly description of an error
func (e *NotImplementedError) Error() string {
return e.Message
}
// OrigError returns original error
func (e *NotImplementedError) OrigError() error {
return e
}
// IsNotImplementedError indicates that this error is of NotImplementedError type
func (e *NotImplementedError) IsNotImplementedError() bool {
return true
}
// Is provides an equivalency check for NotImplementedError to be used with errors.Is
func (e *NotImplementedError) Is(target error) bool {
err, ok := Unwrap(target).(*NotImplementedError)
if !ok {
return false
}
return err.Message == e.Message
}
// IsNotImplemented returns true if `e` contains a [NotImplementedError] in its
// chain.
func IsNotImplemented(e error) bool {
other := &NotImplementedError{}
return errors.As(e, &other)
}
// CompareFailed returns new instance of CompareFailedError
func CompareFailed(message string, args ...interface{}) Error {
return newTrace(&CompareFailedError{
Message: fmt.Sprintf(message, args...),
})
}
// CompareFailedError indicates a failed comparison (e.g. bad password or hash)
type CompareFailedError struct {
// Message is user-friendly error message
Message string `json:"message"`
}
// Error is debug - friendly message
func (e *CompareFailedError) Error() string {
if e.Message != "" {
return e.Message
}
return "compare failed"
}
// OrigError returns original error (in this case this is the error itself)
func (e *CompareFailedError) OrigError() error {
return e
}
// IsCompareFailedError indicates that this is CompareFailedError
func (e *CompareFailedError) IsCompareFailedError() bool {
return true
}
// Is provides an equivalency check for CompareFailedError to be used with errors.Is
func (e *CompareFailedError) Is(target error) bool {
err, ok := Unwrap(target).(*CompareFailedError)
if !ok {
return false
}
return err.Message == e.Message
}
// IsCompareFailed returns true if `e` contains a [CompareFailedError] in its
// chain.
func IsCompareFailed(e error) bool {
other := &CompareFailedError{}
return errors.As(e, &other)
}
// AccessDenied returns new instance of AccessDeniedError
func AccessDenied(message string, args ...interface{}) Error {
return newTrace(&AccessDeniedError{
Message: fmt.Sprintf(message, args...),
})
}
// AccessDeniedError indicates denied access
type AccessDeniedError struct {
Message string `json:"message"`
}
// Error is debug - friendly error message
func (e *AccessDeniedError) Error() string {
if e.Message != "" {
return e.Message
}
return "access denied"
}
// IsAccessDeniedError indicates that this error is of AccessDeniedError type
func (e *AccessDeniedError) IsAccessDeniedError() bool {
return true
}
// OrigError returns original error (in this case this is the error itself)
func (e *AccessDeniedError) OrigError() error {
return e
}
// Is provides an equivalency check for AccessDeniedError to be used with errors.Is
func (e *AccessDeniedError) Is(target error) bool {
err, ok := Unwrap(target).(*AccessDeniedError)
if !ok {
return false
}
return err.Message == e.Message
}
// IsAccessDenied returns true if `e` contains an [AccessDeniedError] in its
// chain.
func IsAccessDenied(e error) bool {
other := &AccessDeniedError{}
return errors.As(e, &other)
}
// ConvertSystemError converts system error to appropriate trace error
// if it is possible, otherwise, returns original error
func ConvertSystemError(err error) error {
innerError := Unwrap(err)
if os.IsExist(innerError) {
return newTrace(&AlreadyExistsError{
Message: innerError.Error(),
})
}
if os.IsNotExist(innerError) {
return newTrace(&NotFoundError{
Message: innerError.Error(),
})
}
if os.IsPermission(innerError) {
return newTrace(&AccessDeniedError{
Message: innerError.Error(),
})
}
switch realErr := innerError.(type) {
case *net.OpError:
return newTrace(&ConnectionProblemError{
Err: realErr,
})
case *os.PathError:
message := fmt.Sprintf("failed to execute command %v error: %v", realErr.Path, realErr.Err)
return newTrace(&AccessDeniedError{
Message: message,
})
case x509.SystemRootsError, x509.UnknownAuthorityError:
return newTrace(&TrustError{Err: innerError})
}
if _, ok := innerError.(net.Error); ok {
return newTrace(&ConnectionProblemError{
Err: innerError,
})
}
return err
}
// ConnectionProblem returns new instance of ConnectionProblemError
func ConnectionProblem(err error, message string, args ...interface{}) Error {
return newTrace(&ConnectionProblemError{
Message: fmt.Sprintf(message, args...),
Err: err,
})
}
// ConnectionProblemError indicates a network related problem
type ConnectionProblemError struct {
Message string `json:"message"`
Err error `json:"-"`
}
// Error is debug - friendly error message
func (c *ConnectionProblemError) Error() string {
if c.Message != "" {
return c.Message
}
return UserMessage(c.Err)
}
// IsConnectionProblemError indicates that this error is of ConnectionProblemError type
func (c *ConnectionProblemError) IsConnectionProblemError() bool {
return true
}
// Unwrap returns the wrapped error if any
func (c *ConnectionProblemError) Unwrap() error {
return c.Err
}
// OrigError returns original error
func (c *ConnectionProblemError) OrigError() error {
if c.Err != nil {
return c.Err
}
return c
}
// Is provides an equivalency check for ConnectionProblemError to be used with errors.Is
func (c *ConnectionProblemError) Is(target error) bool {
err, ok := Unwrap(target).(*ConnectionProblemError)
if !ok {
return false
}
return err.Message == c.Message && err.Err == c.Err
}
// IsConnectionProblem returns true if `e` contains a [ConnectionProblemError]
// in its chain.
func IsConnectionProblem(e error) bool {
other := &ConnectionProblemError{}
return errors.As(e, &other)
}
// LimitExceeded returns whether new instance of LimitExceededError
func LimitExceeded(message string, args ...interface{}) Error {
return newTrace(&LimitExceededError{
Message: fmt.Sprintf(message, args...),
})
}
// LimitExceededError indicates rate limit or connection limit problem
type LimitExceededError struct {
Message string `json:"message"`
}
// Error is debug - friendly error message
func (e *LimitExceededError) Error() string {
return e.Message
}
// IsLimitExceededError indicates that this error is of ConnectionProblem
func (e *LimitExceededError) IsLimitExceededError() bool {
return true
}
// OrigError returns original error (in this case this is the error itself)
func (e *LimitExceededError) OrigError() error {
return e
}
// Is provides an equivalency check for LimitExceededError to be used with errors.Is
func (e *LimitExceededError) Is(target error) bool {
err, ok := Unwrap(target).(*LimitExceededError)
if !ok {
return false
}
return err.Message == e.Message
}
// IsLimitExceeded returns true if `e` contains a [LimitExceededError] in its
// chain.
func IsLimitExceeded(e error) bool {
other := &LimitExceededError{}
return errors.As(e, &other)
}
// Trust returns new instance of TrustError
func Trust(err error, message string, args ...interface{}) Error {
return newTrace(&TrustError{
Message: fmt.Sprintf(message, args...),
Err: err,
})
}
// TrustError indicates trust-related validation error (e.g. untrusted cert)
type TrustError struct {
// Err is original error
Err error `json:"-"`
Message string `json:"message"`
}
// Error returns log-friendly error description
func (t *TrustError) Error() string {
if t.Message != "" {
return t.Message
}
return UserMessage(t.Err)
}
// IsTrustError indicates that this error is of TrustError type
func (*TrustError) IsTrustError() bool {
return true
}
// Unwrap returns the wrapped error if any
func (t *TrustError) Unwrap() error {
return t.Err
}
// OrigError returns original error (in this case this is the error itself)
func (t *TrustError) OrigError() error {
if t.Err != nil {
return t.Err
}
return t
}
// Is provides an equivalency check for TrustError to be used with errors.Is
func (t *TrustError) Is(target error) bool {
err, ok := Unwrap(target).(*TrustError)
if !ok {
return false
}
return err.Message == t.Message && err.Err == t.Err
}
// IsTrustError returns true if `e` contains a [TrustError] in its chain.
func IsTrustError(e error) bool {
other := &TrustError{}
return errors.As(e, &other)
}
// OAuth2 returns new instance of OAuth2Error
func OAuth2(code, message string, query url.Values) Error {
return newTrace(&OAuth2Error{
Code: code,
Message: message,
Query: query,
})
}
// OAuth2Error defined an error used in OpenID Connect Flow (OIDC)
type OAuth2Error struct {
Code string `json:"code"`
Message string `json:"message"`
Query url.Values `json:"query"`
}
// Error returns log friendly description of an error
func (o *OAuth2Error) Error() string {
return fmt.Sprintf("OAuth2 error code=%v, message=%v", o.Code, o.Message)
}
// IsOAuth2Error returns whether this error of OAuth2Error type
func (o *OAuth2Error) IsOAuth2Error() bool {
return true
}
// Is provides an equivalency check for OAuth2Error to be used with errors.Is
func (o *OAuth2Error) Is(target error) bool {
err, ok := Unwrap(target).(*OAuth2Error)
if !ok {
return false
}
if err.Message != o.Message ||
err.Code != o.Code ||
len(err.Query) != len(o.Query) {
return false
}
for k, v := range err.Query {
for k2, v2 := range o.Query {
if k != k2 && len(v) != len(v2) {
return false
}
for i := range v {
if v[i] != v2[i] {
return false
}
}
}
}
return true
}
// IsOAuth2 returns true if `e` contains an [OAuth2Error] in its chain.
func IsOAuth2(e error) bool {
other := &OAuth2Error{}
return errors.As(e, &other)
}
// IsEOF returns true if the passed error is io.EOF.
// Deprecated: Use [errors.Is] instead.
func IsEOF(e error) bool {
return errors.Is(Unwrap(e), io.EOF)
}
// Retry returns new instance of RetryError which indicates a transient error type
func Retry(err error, message string, args ...interface{}) Error {
return newTrace(&RetryError{
Message: fmt.Sprintf(message, args...),
Err: err,
})
}
// RetryError indicates a transient error type
type RetryError struct {
Message string `json:"message"`
Err error `json:"-"`
}
// Error is debug-friendly error message
func (c *RetryError) Error() string {
if c.Message != "" {
return c.Message
}
return UserMessage(c.Err)
}
// IsRetryError indicates that this error is of RetryError type
func (c *RetryError) IsRetryError() bool {
return true
}
// Unwrap returns the wrapped error if any
func (c *RetryError) Unwrap() error {
return c.Err
}
// OrigError returns original error (in this case this is the error itself)
func (c *RetryError) OrigError() error {
if c.Err != nil {
return c.Err
}
return c
}
// Is provides an equivalency check for RetryError to be used with errors.Is
func (c *RetryError) Is(target error) bool {
err, ok := Unwrap(target).(*RetryError)
if !ok {
return false
}
return err.Message == c.Message && err.Err == c.Err
}
// IsRetryError returns true if `e` contains a [RetryError] in its chain.
func IsRetryError(e error) bool {
other := &RetryError{}
return errors.As(e, &other)
}