-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathretryable.go
121 lines (101 loc) · 2.59 KB
/
retryable.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
package errorutil
import (
"errors"
"fmt"
"net/http"
)
// Retryabler defines an error that may be temporary. A function returning a retryable error should be executed again.
type Retryabler interface {
Retryable() bool
}
// IsRetryable checks if an error is retryable (i.e. implements Retryabler and Retryable returns true).
//
// If the error is nil or does not implement Retryabler, false is returned.
func IsRetryable(err error) bool {
type causer interface {
Cause() error
}
for err != nil {
if retry, ok := err.(Retryabler); ok {
return retry.Retryable()
}
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
return false
}
// IsNotRetryable checks if an error is explicitly marked as not retryable (i.e. implements Retryabler and Retryable returns false).
//
// If the error is nil or does not implement Retryabler, false is returned.
func IsNotRetryable(err error) bool {
type causer interface {
Cause() error
}
for err != nil {
if retry, ok := err.(Retryabler); ok {
return !retry.Retryable()
}
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
return false
}
// RetryableError marks an error as retryable. It returns nil if the error is nil.
func RetryableError(err error) error {
if err == nil {
return nil
}
return &retryableError{err: err}
}
// NotRetryableError marks an error as NOT retryable. It returns nil if the error is nil.
func NotRetryableError(err error) error {
if err == nil {
return nil
}
return ¬RetryableError{err: err}
}
// NewRetryableError returns a retryable error that formats as the given text.
func NewRetryableError(text string) error {
return RetryableError(errors.New(text))
}
// NewRetryableErrorf formats according to a format specifier and returns the string
// as a value that satisfies a retryable error.
func NewRetryableErrorf(format string, args ...interface{}) error {
return RetryableError(fmt.Errorf(format, args...))
}
type retryableError struct {
err error
}
func (err *retryableError) Error() string {
return err.err.Error()
}
func (err *retryableError) Retryable() bool {
return true
}
func (err *retryableError) HTTPStatusCode() int {
return http.StatusInternalServerError
}
func (err *retryableError) Cause() error {
return err.err
}
type notRetryableError struct {
err error
}
func (err *notRetryableError) Error() string {
return err.err.Error()
}
func (err *notRetryableError) Retryable() bool {
return false
}
func (err *notRetryableError) HTTPStatusCode() int {
return http.StatusInternalServerError
}
func (err *notRetryableError) Cause() error {
return err.err
}