-
Notifications
You must be signed in to change notification settings - Fork 20
/
error.go
51 lines (43 loc) · 1.14 KB
/
error.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
package runctl
import (
"errors"
)
type recoverableInfoError struct {
err error
recoverable bool
}
// RecoverabilityInfo is exposed by errors that can be converted to an RecoverabilityInfo object
type RecoverabilityInfo interface {
error
IsRecoverable() bool
}
// NewRecoverabilityInfoError creates a new error implementing the RecoverabilityInfo interface
func NewRecoverabilityInfoError(err error, recoverable bool) RecoverabilityInfo {
if err == nil {
panic("Cannot use nil as error")
}
return &recoverableInfoError{err: err,
recoverable: recoverable,
}
}
// Error returns error string of the provided error
func (err *recoverableInfoError) Error() string {
return err.err.Error()
}
// IsRecoverable returns true if error can be recovered from
func (err *recoverableInfoError) IsRecoverable() bool {
if err == nil {
return false
}
return err.recoverable
}
// IsRecoverable returns true if error can be recovered from
func IsRecoverable(err error) bool {
if err == nil {
return false
}
if recoverability := RecoverabilityInfo(nil); errors.As(err, &recoverability) {
return recoverability.IsRecoverable()
}
return false
}