-
Notifications
You must be signed in to change notification settings - Fork 0
/
into.go
43 lines (36 loc) 路 894 Bytes
/
into.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
package terrors
import (
"errors"
"slices"
)
// Into finds the first error in err's chain that matches target type T, and if so, returns it.
//
// Into is type-safe alternative to As.
func Into[T error](err error) (val T, ok bool) {
ok = errors.As(err, &val)
return val, ok
}
type RecoveryInfo struct {
DeepestSimpleErrorMessage string
Suggestion string
}
func IsRecoverable(err error) (bool, *RecoveryInfo) {
chain := GetChain(err)
// we want to get the deepest recoverable error in the chain
slices.Reverse(chain)
for _, e := range chain {
if werr, ok := e.(*wrapError); ok {
if werr.recovery != nil {
msg := werr.msg
if werr.err != nil {
msg += ": " + werr.err.Error()
}
return true, &RecoveryInfo{
DeepestSimpleErrorMessage: msg,
Suggestion: werr.recovery.Suggestion,
}
}
}
}
return false, nil
}