-
Notifications
You must be signed in to change notification settings - Fork 1
/
recover.go
102 lines (75 loc) · 1.77 KB
/
recover.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
//
// Copyright (C) 2020 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/errors
//
package faults
import (
"errors"
"time"
)
type Timeout interface{ Timeout() time.Duration }
func IsTimeout(err error, deadline time.Duration) bool {
var e interface{ Timeout() time.Duration }
ok := errors.As(err, &e)
return ok && e.Timeout() >= deadline
}
type NotFound interface{ NotFound() string }
func IsNotFound(err error, key ...string) bool {
var e interface{ NotFound() string }
if ok := errors.As(err, &e); !ok {
return false
}
if len(key) == 0 {
return e.NotFound() != ""
}
for _, x := range key {
if e.NotFound() == x {
return true
}
}
return false
}
type StatusCode interface{ StatusCode() string }
func IsStatusCode(err error, code ...string) bool {
var e interface{ StatusCode() string }
if ok := errors.As(err, &e); !ok {
return false
}
if len(code) == 0 {
return e.StatusCode() != ""
}
for _, x := range code {
if e.StatusCode() == x {
return true
}
}
return false
}
type PreConditionFailed interface{ PreConditionFailed() bool }
func IsPreConditionFailed(err error) bool {
var e interface{ PreConditionFailed() bool }
ok := errors.As(err, &e)
return ok && e.PreConditionFailed()
}
type Conflict interface{ Conflict() bool }
func IsConflict(err error) bool {
var e interface{ Conflict() bool }
ok := errors.As(err, &e)
return ok && e.Conflict()
}
type Gone interface{ Gone() bool }
func IsGone(err error) bool {
var e interface{ Gone() bool }
ok := errors.As(err, &e)
return ok && e.Gone()
}
type Issue interface {
ErrCode() string
ErrType() string
ErrInstance() string
ErrTitle() string
ErrDetail() string
}