forked from gravitational/trace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
526 lines (450 loc) · 12.9 KB
/
errors.go
File metadata and controls
526 lines (450 loc) · 12.9 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
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
/*
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"
"fmt"
"io"
"net"
"net/url"
"os"
)
// NotFound returns new instance of not found error
func NotFound(message string, args ...interface{}) Error {
return newTrace(&NotFoundError{
Message: fmt.Sprintf(message, args...),
}, 2)
}
// 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
}
// IsNotFound returns whether this error is of NotFoundError type
func IsNotFound(err error) bool {
err = Unwrap(err)
_, ok := err.(interface {
IsNotFoundError() bool
})
if !ok {
return os.IsNotExist(err)
}
return true
}
// AlreadyExists returns a new instance of AlreadyExists error
func AlreadyExists(message string, args ...interface{}) Error {
return newTrace(&AlreadyExistsError{
Message: fmt.Sprintf(message, args...),
}, 2)
}
// 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 (n *AlreadyExistsError) Error() string {
if n.Message != "" {
return n.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
}
// IsAlreadyExists returns whether this is error indicating that object
// already exists
func IsAlreadyExists(e error) bool {
type ae interface {
IsAlreadyExistsError() bool
}
_, ok := Unwrap(e).(ae)
return ok
}
// BadParameter returns a new instance of BadParameterError
func BadParameter(message string, args ...interface{}) Error {
return newTrace(&BadParameterError{
Message: fmt.Sprintf(message, args...),
}, 2)
}
// 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
}
// IsBadParameter returns whether this error is of BadParameterType
func IsBadParameter(e error) bool {
type bp interface {
IsBadParameterError() bool
}
_, ok := Unwrap(e).(bp)
return ok
}
// NotImplemented returns a new instance of NotImplementedError
func NotImplemented(message string, args ...interface{}) Error {
return newTrace(&NotImplementedError{
Message: fmt.Sprintf(message, args...),
}, 2)
}
// 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
}
// IsNotImplemented returns whether this error is of NotImplementedError type
func IsNotImplemented(e error) bool {
type ni interface {
IsNotImplementedError() bool
}
err, ok := Unwrap(e).(ni)
return ok && err.IsNotImplementedError()
}
// CompareFailed returns new instance of CompareFailedError
func CompareFailed(message string, args ...interface{}) Error {
return newTrace(&CompareFailedError{
Message: fmt.Sprintf(message, args...),
}, 2)
}
// 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
}
// IsCompareFailed detects if this error is of CompareFailed type
func IsCompareFailed(e error) bool {
type cf interface {
IsCompareFailedError() bool
}
_, ok := Unwrap(e).(cf)
return ok
}
// AccessDenied returns new instance of AccessDeniedError
func AccessDenied(message string, args ...interface{}) Error {
return newTrace(&AccessDeniedError{
Message: fmt.Sprintf(message, args...),
}, 2)
}
// 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
}
// IsAccessDenied detects if this error is of AccessDeniedError type
func IsAccessDenied(err error) bool {
_, ok := Unwrap(err).(interface {
IsAccessDeniedError() bool
})
return ok
}
// 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(),
}, 2)
}
if os.IsNotExist(innerError) {
return newTrace(&NotFoundError{
Message: innerError.Error(),
}, 2)
}
if os.IsPermission(innerError) {
return newTrace(&AccessDeniedError{
Message: innerError.Error(),
}, 2)
}
switch realErr := innerError.(type) {
case *net.OpError:
return newTrace(&ConnectionProblemError{
Err: realErr,
}, 2)
case *os.PathError:
message := fmt.Sprintf("failed to execute command %v error: %v", realErr.Path, realErr.Err)
return newTrace(&AccessDeniedError{
Message: message,
}, 2)
case x509.SystemRootsError, x509.UnknownAuthorityError:
return newTrace(&TrustError{Err: innerError}, 2)
}
if _, ok := innerError.(net.Error); ok {
return newTrace(&ConnectionProblemError{
Err: innerError,
}, 2)
}
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,
}, 2)
}
// 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
}
// IsConnectionProblem returns whether this error is of ConnectionProblemError
func IsConnectionProblem(e error) bool {
type ad interface {
IsConnectionProblemError() bool
}
_, ok := Unwrap(e).(ad)
return ok
}
// LimitExceeded returns whether new instance of LimitExceededError
func LimitExceeded(message string, args ...interface{}) Error {
return newTrace(&LimitExceededError{
Message: fmt.Sprintf(message, args...),
}, 2)
}
// LimitExceededError indicates rate limit or connection limit problem
type LimitExceededError struct {
Message string `json:"message"`
}
// Error is debug - friendly error message
func (c *LimitExceededError) Error() string {
return c.Message
}
// IsLimitExceededError indicates that this error is of ConnectionProblem
func (c *LimitExceededError) IsLimitExceededError() bool {
return true
}
// OrigError returns original error (in this case this is the error itself)
func (c *LimitExceededError) OrigError() error {
return c
}
// IsLimitExceeded detects if this error is of LimitExceededError
func IsLimitExceeded(e error) bool {
type ad interface {
IsLimitExceededError() bool
}
_, ok := Unwrap(e).(ad)
return ok
}
// 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,
}, 2)
}
// 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
}
// IsTrustError returns if this is a trust error
func IsTrustError(e error) bool {
type te interface {
IsTrustError() bool
}
_, ok := Unwrap(e).(te)
return ok
}
// OAuth2 returns new instance of OAuth2Error
func OAuth2(code, message string, query url.Values) Error {
return newTrace(&OAuth2Error{
Code: code,
Message: message,
Query: query,
}, 2)
}
// 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
}
// IsOAuth2 returns if this is a OAuth2-related error
func IsOAuth2(e error) bool {
type oe interface {
IsOAuth2Error() bool
}
_, ok := Unwrap(e).(oe)
return ok
}
// IsEOF returns true if the passed error is io.EOF
func IsEOF(e error) bool {
return 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,
}, 2)
}
// 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
}
// IsRetryError returns whether this error is of ConnectionProblemError
func IsRetryError(e error) bool {
type ad interface {
IsRetryError() bool
}
_, ok := Unwrap(e).(ad)
return ok
}