-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
253 lines (201 loc) · 4.76 KB
/
functions.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
package errutil
import (
"errors"
)
type CheckerFn func(error) bool
// IsAccessDenied checks if an error exhibits AccessDenier behavior
func IsAccessDenied(err error) bool {
if err == nil {
return false
}
var e AccessDenier
if errors.As(err, &e) && e.AccessDenied() {
return true
}
return IsAccessDenied(errors.Unwrap(err))
}
// IsConflict checks if an error exhibits Conflicter behavior
func IsConflict(err error) bool {
if err == nil {
return false
}
var e Conflicter
if errors.As(err, &e) && e.Conflict() {
return true
}
return IsConflict(errors.Unwrap(err))
}
// IsExist checks if an error exhibits Exister behavior
func IsExist(err error) bool {
if err == nil {
return false
}
var e Exister
if errors.As(err, &e) && e.Exists() {
return true
}
return IsExist(errors.Unwrap(err))
}
// IsNotFound checks if an error exhibits NotFounder behavior
func IsNotFound(err error) bool {
if err == nil {
return false
}
var e NotFounder
if errors.As(err, &e) && e.NotFound() {
return true
}
return IsNotFound(errors.Unwrap(err))
}
// IsRateLimit checks if an error exhibits RateLimiter behavior
func IsRateLimit(err error) bool {
if err == nil {
return false
}
var e RateLimiter
if errors.As(err, &e) && e.RateLimit() {
return true
}
return IsRateLimit(errors.Unwrap(err))
}
// IsTemporary checks if an error exhibits Temporary behavior
func IsTemporary(err error) bool {
if err == nil {
return false
}
var e Temporarily
if errors.As(err, &e) && e.Temporary() {
return true
}
return IsTemporary(errors.Unwrap(err))
}
// IsStatusCodeable checks if an error exhibits StatusCodeable behavior
func IsStatusCodeable(err error) bool {
var e StatusCodeable
if err == nil || !errors.As(err, &e) {
return false
}
return true
}
// IsCodeable checks if an error exhibits Codeable behavior
func IsCodeable(err error) bool {
var e Codeable
if err == nil || !errors.As(err, &e) {
return false
}
return true
}
// GetStatusCode returns the status code for an error if it has one
func GetStatusCode(err error) int {
var e StatusCodeable
if err == nil || !errors.As(err, &e) {
return 0
}
return e.StatusCode()
}
// GetCode returns the code for an error if it has one
func GetCode(err error) string {
var e Codeable
if err == nil || !errors.As(err, &e) {
return ""
}
return e.Code()
}
// IsTooLarge checks if an error exhibits TooLarger behavior
func IsTooLarge(err error) bool {
if err == nil {
return false
}
var e TooLarge
if errors.As(err, &e) && e.TooLarge() {
return true
}
return IsTooLarge(errors.Unwrap(err))
}
// IsTooManyRequests checks if an error exhibits TooManyRequester behavior
func IsTooManyRequests(err error) bool {
if err == nil {
return false
}
var e TooManyRequester
if errors.As(err, &e) && e.TooManyRequests() {
return true
}
return IsTooManyRequests(errors.Unwrap(err))
}
// IsInternalErrorMessage checks if an error exhibits InternalErrorMessagable behavior
func IsInternalErrorMessage(err error) bool {
var e InternalErrorMessagable
if err == nil || !errors.As(err, &e) {
return false
}
return true
}
// GetInternalErrorMessage returns the internal error message for an error if it has one
func GetInternalErrorMessage(err error) string {
var e InternalErrorMessagable
if err == nil || !errors.As(err, &e) {
return ""
}
return e.InternalErrorMessage()
}
// IsTaggable checks if an error exhibits taggable behavior
func IsTaggable(err error) bool {
if err == nil {
return false
}
var e Taggable
if errors.As(err, &e) && len(e.Tags()) > 0 {
return true
}
return IsTaggable(errors.Unwrap(err))
}
// IsStackTraceable checks if an error exhibits taggable behavior
func IsStackTraceable(err error) bool {
if err == nil {
return false
}
var e StackTraceable
if errors.As(err, &e) {
return true
}
return IsStackTraceable(errors.Unwrap(err))
}
// IsUnauthorized checks if an error exhibits Unauthorized behavior
func IsUnauthorized(err error) bool {
if err == nil {
return false
}
var e Unauthorizable
if errors.As(err, &e) && e.Unauthorized() {
return true
}
return IsUnauthorized(errors.Unwrap(err))
}
// GetTags returns all the tags for a given error
func GetTags(err error) []Tag {
var tags []Tag
for {
if err == nil {
break
}
var e Tagged
if errors.As(err, &e) && len(e.Tags()) > 0 {
tags = append(tags, e.Tags()...)
}
err = errors.Unwrap(e)
}
return tags
}
func WithEasyTags(key, value string, additionalKvs ...string) OptsFunc {
return func(opts *Opts) {
tags := make([]Tag, 0, 10)
tags = append(tags, Tag{Key: key, Value: value})
if len(additionalKvs) > 0 && len(additionalKvs)%2 == 0 {
for i := 0; i < len(additionalKvs); i = i + 2 {
tags = append(tags, Tag{Key: additionalKvs[i], Value: additionalKvs[i+1]})
}
}
opts.Tags = tags
}
}