/
recoverability.go
70 lines (60 loc) · 1.7 KB
/
recoverability.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
package errors
import (
"errors"
)
type recoverabilityAnnotation struct {
wrapped error
recoverable bool
}
// let compiler verify interface compliance
var _ error = (*recoverabilityAnnotation)(nil)
func (a *recoverabilityAnnotation) Error() string {
return a.wrapped.Error()
}
func (a *recoverabilityAnnotation) Unwrap() error {
return a.wrapped
}
// errors.Is() would work without this method, but it
// provides a shortcut in case target is the wrapped error.
func (a *recoverabilityAnnotation) Is(target error) bool {
return errors.Is(a.wrapped, target)
}
// Recoverable annotates a given error as recoverable.
// It is equivalent to `RecoverableIf(err, true)`
func Recoverable(err error) error {
return RecoverableIf(err, true)
}
// NonRecoverable annotates a given error as non-recoverable.
// It is equivalent to `RecoverableIf(err, false)`
func NonRecoverable(err error) error {
return RecoverableIf(err, false)
}
// RecoverableIf conditionally annotates a given error as recoverable.
// If err is nil, the function returns nil.
// If the recoverability status of err is equal to cond, the function
// returns err itself.
// Otherwise a new error is returned that wraps err.
func RecoverableIf(err error, cond bool) error {
if err == nil {
return nil
}
if IsRecoverable(err) == cond {
// don't wrap if not necessary
return err
}
return &recoverabilityAnnotation{
wrapped: err,
recoverable: cond,
}
}
// IsRecoverable returns true if the given error has been marked as
// recoverable.
func IsRecoverable(err error) bool {
if err == nil {
return false
}
if annotation := (*recoverabilityAnnotation)(nil); errors.As(err, &annotation) {
return annotation.recoverable
}
return false
}