-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorclass.go
More file actions
300 lines (255 loc) · 6.79 KB
/
Copy patherrorclass.go
File metadata and controls
300 lines (255 loc) · 6.79 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
package rhttp
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"net"
"net/url"
"syscall"
)
// ErrorKind represents the category of an HTTP client error.
type ErrorKind int
const (
// ErrKindUnknown is an unclassified error.
ErrKindUnknown ErrorKind = iota
// ErrKindTimeout indicates the request timed out.
ErrKindTimeout
// ErrKindCanceled indicates the request was canceled by the caller.
ErrKindCanceled
// ErrKindConnection indicates a connection error (refused, reset, etc.).
ErrKindConnection
// ErrKindDNS indicates a transient DNS resolution failure.
ErrKindDNS
// ErrKindTLS indicates a TLS/SSL error.
ErrKindTLS
// ErrKindDNSNotFound indicates the name does not exist (NXDOMAIN). Unlike
// ErrKindDNS this is permanent: retrying the same name cannot succeed.
ErrKindDNSNotFound
// ErrKindCircuitOpen indicates the client's own circuit breaker refused the
// call: the request never reached the network.
ErrKindCircuitOpen
// ErrKindRateLimited indicates the client's own quota refused the call: the
// request never reached the network.
ErrKindRateLimited
)
// String returns a human-readable name for the error kind.
func (k ErrorKind) String() string {
switch k {
case ErrKindTimeout:
return "timeout"
case ErrKindCanceled:
return "canceled"
case ErrKindConnection:
return "connection"
case ErrKindDNS:
return "dns"
case ErrKindDNSNotFound:
return "dns_not_found"
case ErrKindTLS:
return "tls"
case ErrKindCircuitOpen:
return "circuit_open"
case ErrKindRateLimited:
return "rate_limited"
default:
return "unknown"
}
}
// IsRetryable returns true if the error kind is typically safe to retry.
//
// ErrKindCircuitOpen and ErrKindRateLimited are deliberately absent: both are
// the client protecting itself, and retrying inside the same operation defeats
// the protection that produced them.
func (k ErrorKind) IsRetryable() bool {
switch k {
case ErrKindTimeout, ErrKindConnection, ErrKindDNS:
return true
default:
return false
}
}
// ClassifiedError wraps an error with its classification.
type ClassifiedError struct {
Kind ErrorKind
Err error
}
// Error returns a string representation of the classified error.
func (e *ClassifiedError) Error() string {
if e.Err == nil {
return e.Kind.String() + " error"
}
return e.Kind.String() + ": " + e.Err.Error()
}
// Unwrap returns the underlying error, allowing use with errors.Is and errors.As.
func (e *ClassifiedError) Unwrap() error {
return e.Err
}
// Classify analyzes an error and returns its classification.
func Classify(err error) *ClassifiedError {
if err == nil {
return nil
}
kind := classifyError(err)
return &ClassifiedError{
Kind: kind,
Err: err,
}
}
func classifyTLS(err error) ErrorKind {
var certErr *tls.CertificateVerificationError
if errors.As(err, &certErr) {
return ErrKindTLS
}
var unknownAuthErr x509.UnknownAuthorityError
if errors.As(err, &unknownAuthErr) {
return ErrKindTLS
}
var invalidCertErr x509.CertificateInvalidError
if errors.As(err, &invalidCertErr) {
return ErrKindTLS
}
var hostnameErr x509.HostnameError
if errors.As(err, &hostnameErr) {
return ErrKindTLS
}
return ErrKindUnknown
}
func classifyConnection(err error) ErrorKind {
if errors.Is(err, syscall.ECONNREFUSED) ||
errors.Is(err, syscall.ECONNRESET) ||
errors.Is(err, syscall.EHOSTUNREACH) ||
errors.Is(err, syscall.ENETUNREACH) {
return ErrKindConnection
}
var opErr *net.OpError
if errors.As(err, &opErr) {
if opErr.Op == "dial" || opErr.Op == "read" || opErr.Op == "write" {
return ErrKindConnection
}
}
return ErrKindUnknown
}
// classifySentinel matches one of the package's own sentinels by identity: two
// interface comparisons, cheap enough to run before any transport inspection,
// and unwrapped is how these two travel in practice.
func classifySentinel(err error) ErrorKind {
switch err {
case ErrCircuitOpen:
return ErrKindCircuitOpen
case ErrRateLimited:
return ErrKindRateLimited
default:
return ErrKindUnknown
}
}
// classifyWrappedSentinel matches a sentinel anywhere in the error chain. It
// walks, so it runs only once every transport branch has missed.
func classifyWrappedSentinel(err error) ErrorKind {
if errors.Is(err, ErrCircuitOpen) {
return ErrKindCircuitOpen
}
if errors.Is(err, ErrRateLimited) {
return ErrKindRateLimited
}
return ErrKindUnknown
}
func classifyError(err error) ErrorKind {
if err == nil {
return ErrKindUnknown
}
if kind := classifySentinel(err); kind != ErrKindUnknown {
return kind
}
if errors.Is(err, context.DeadlineExceeded) {
return ErrKindTimeout
}
if errors.Is(err, context.Canceled) {
return ErrKindCanceled
}
var urlErr *url.Error
if errors.As(err, &urlErr) {
if urlErr.Timeout() {
return ErrKindTimeout
}
if urlErr.Err != nil {
return classifyError(urlErr.Err)
}
}
var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() {
return ErrKindTimeout
}
var dnsErr *net.DNSError
if errors.As(err, &dnsErr) {
if dnsErr.IsNotFound {
return ErrKindDNSNotFound
}
return ErrKindDNS
}
if kind := classifyTLS(err); kind != ErrKindUnknown {
return kind
}
if kind := classifyConnection(err); kind != ErrKindUnknown {
return kind
}
return classifyWrappedSentinel(err)
}
// IsTimeout returns true if the error is a timeout error.
func IsTimeout(err error) bool {
if err == nil {
return false
}
classified := Classify(err)
return classified.Kind == ErrKindTimeout
}
// IsCanceled returns true if the error is a cancellation error.
func IsCanceled(err error) bool {
if err == nil {
return false
}
classified := Classify(err)
return classified.Kind == ErrKindCanceled
}
// IsConnection returns true if the error is a connection error.
func IsConnection(err error) bool {
if err == nil {
return false
}
classified := Classify(err)
return classified.Kind == ErrKindConnection
}
// IsDNS returns true if the error is a DNS error, whether transient or
// permanent. Use IsDNSNotFound to single out the permanent case.
func IsDNS(err error) bool {
if err == nil {
return false
}
classified := Classify(err)
return classified.Kind == ErrKindDNS || classified.Kind == ErrKindDNSNotFound
}
// IsDNSNotFound returns true if the name does not exist (NXDOMAIN). Such an
// error is permanent and is never retried by DefaultIsRetryable.
func IsDNSNotFound(err error) bool {
if err == nil {
return false
}
classified := Classify(err)
return classified.Kind == ErrKindDNSNotFound
}
// IsTLS returns true if the error is a TLS error.
func IsTLS(err error) bool {
if err == nil {
return false
}
classified := Classify(err)
return classified.Kind == ErrKindTLS
}
// IsRetryable returns true if the error is typically safe to retry.
func IsRetryable(err error) bool {
if err == nil {
return false
}
classified := Classify(err)
return classified.Kind.IsRetryable()
}