-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecovererr_test.go
108 lines (88 loc) · 2.43 KB
/
recovererr_test.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
package recovererr
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDoRecover(t *testing.T) {
var (
ConnectionError = errors.New("connection error")
ParseError = errors.New("parse error")
)
t.Run("recoverable wrapped error", func(t *testing.T) {
err := Recoverable(ConnectionError)
found, recover := DoRecover(err)
assert.True(t, found, err)
assert.True(t, recover, err)
})
t.Run("unrecoverable wrapped error", func(t *testing.T) {
err := Unrecoverable(ParseError)
found, recover := DoRecover(err)
assert.True(t, found, err)
assert.False(t, recover, err)
})
t.Run("recoverable wrapped error wrapped", func(t *testing.T) {
err := fmt.Errorf("failed to store object, %w", Recoverable(ConnectionError))
found, recover := DoRecover(err)
assert.True(t, found, err)
assert.True(t, recover, err)
})
t.Run("unrecoverable wrapped error", func(t *testing.T) {
err := fmt.Errorf("failed to parse object, %w", Unrecoverable(ParseError))
found, recover := DoRecover(err)
assert.True(t, found, err)
assert.False(t, recover, err)
})
t.Run("unrecoverable wrapped error", func(t *testing.T) {
err := &anyError{}
found, recover := DoRecover(err)
assert.False(t, found, err)
assert.False(t, recover, err)
})
t.Run("other recover error implementation", func(t *testing.T) {
err := &otherRecoverError{recover: true}
found, recover := DoRecover(err)
assert.True(t, found, err)
assert.True(t, recover, err)
})
}
func TestRecoverable(t *testing.T) {
t.Run("nil error", func(t *testing.T) {
defer func() {
assert.NotNil(t, recover(), "expected pacic")
}()
Recoverable(nil)
})
t.Run("not-nil error", func(t *testing.T) {
defer func() {
assert.Nil(t, recover(), "expected no panic")
}()
Recoverable(errors.New("not-nil error"))
})
}
func TestUnrecoverable(t *testing.T) {
t.Run("nil error", func(t *testing.T) {
defer func() {
assert.NotNil(t, recover(), "expected pacic")
}()
Unrecoverable(nil)
})
t.Run("not-nil error", func(t *testing.T) {
defer func() {
assert.Nil(t, recover(), "expected no panic")
}()
Unrecoverable(errors.New("not-nil error"))
})
}
type anyError struct{}
func (ae anyError) Error() string { return "" }
type otherRecoverError struct {
recover bool
}
func (ore otherRecoverError) Error() string {
return fmt.Sprintf("recover: %t", ore.recover)
}
func (ore otherRecoverError) Recover() bool {
return ore.recover
}