-
Notifications
You must be signed in to change notification settings - Fork 5
/
doc.go
60 lines (44 loc) · 1.47 KB
/
doc.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
/*
Package errorutil allows errors to be tagged, allowing calling code to make decisions,
such as "is the error retryable ?" or "what kind of HTTP status code should I return ?".
Retryable errors
Mark errors as retryable :
func foo() error {
err := bar()
if err != nil {
// should be retried
return errorutil.RetryableError(err)
}
return baz() // should not be retried
}
func main() {
err := foo()
if errorutil.IsRetryable(err) {
// retry !
}
}
HTTP Aware errors
Build an error based on a http.Response. It will be retryable of status code is http.StatusBadGateway, http.StatusGatewayTimeout, http.StatusServiceUnavailable, http.StatusInternalServerError or 429 (Too many requests).
resp, err := http.Get("http://www.example.com")
if err != nil {
// return error
}
defer resp.Body.Close()
if err := errorutil.HTTPError(resp); err != nil {
// return error
}
// handle response
Find the most appropriate status code for an error :
w.WriteHeader(errorutil.HTTPStatusCode(err))
Generate specific error types :
err := errors.New("some error")
err = errorutil.NotFoundError(err)
w.WriteHeader(errorutil.HTTPStatusCode(err)) // returns http.StatusNotFound
Exponential backoff
see backoffutil sub package
Notes
errorutil is compatible with https://github.com/objenious/errors :
err = errors.Wrap(errorutil.RetryableError(err), "some message")
errorutil.IsRetryable(err) // returns true
*/
package errorutil