-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain_test.go
176 lines (152 loc) · 2.96 KB
/
chain_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package oops_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"github.com/calebcase/oops"
"github.com/stretchr/testify/require"
)
type closer struct {
open bool
}
func open() (*closer, error) {
return &closer{true}, nil
}
func (c *closer) Close(ctx context.Context) error {
c.open = false
return oops.New("close error")
}
func openclose() (err error) {
ctx := context.Background()
f, err := open()
if err != nil {
return err
}
defer func() {
err = oops.Chain(err, f.Close(ctx))
}()
return oops.New("openclose error")
}
func opencloseP() (err error) {
ctx := context.Background()
f, err := open()
if err != nil {
return err
}
defer func() {
oops.ChainP(&err, f.Close(ctx))
}()
return oops.New("openclose error")
}
func TestChain(t *testing.T) {
t.Run("Chain", func(t *testing.T) {
err := openclose()
werr := errors.Unwrap(err)
t.Logf("error: %T\n%+v\n", err, err)
require.Error(t, err)
t.Logf("wrapped error: %T\n%+v\n", werr, werr)
require.Error(t, werr)
bs, jerr := json.MarshalIndent(err, "", " ")
require.NoError(t, jerr)
t.Log(string(bs))
})
t.Run("ChainP", func(t *testing.T) {
err := opencloseP()
werr := errors.Unwrap(err)
t.Logf("error: %T\n%+v\n", err, err)
require.Error(t, err)
t.Logf("wrapped error: %T\n%+v\n", werr, werr)
require.Error(t, werr)
})
// Chain should "fold" away nils and remove itself if the chain only
// contains one element.
t.Run("folding", func(t *testing.T) {
type TC struct {
Errs []error
Expect error
}
e0 := errors.New("0")
e1 := errors.New("1")
e2 := errors.New("2")
tcs := []TC{
{
Errs: nil,
Expect: nil,
},
{
Errs: []error{},
Expect: nil,
},
{
Errs: []error{nil},
Expect: nil,
},
{
Errs: []error{e0},
Expect: e0,
},
{
Errs: []error{nil, nil},
Expect: nil,
},
{
Errs: []error{nil, e1},
Expect: e1,
},
{
Errs: []error{e0, nil},
Expect: e0,
},
{
Errs: []error{e0, e1},
Expect: oops.ChainError{e0, e1},
},
{
Errs: []error{nil, nil, nil},
Expect: nil,
},
{
Errs: []error{nil, nil, e2},
Expect: e2,
},
{
Errs: []error{nil, e1, nil},
Expect: e1,
},
{
Errs: []error{nil, e1, e2},
Expect: oops.ChainError{e1, e2},
},
{
Errs: []error{e0, nil, nil},
Expect: e0,
},
{
Errs: []error{e0, nil, e2},
Expect: oops.ChainError{e0, e2},
},
{
Errs: []error{e0, e1, nil},
Expect: oops.ChainError{e0, e1},
},
{
Errs: []error{e0, e1, e2},
Expect: oops.ChainError{e0, e1, e2},
},
}
for _, tc := range tcs {
t.Run(fmt.Sprintf("%s", tc.Errs), func(t *testing.T) {
t.Run("Chain", func(t *testing.T) {
require.Equal(t, tc.Expect, oops.Chain(tc.Errs...))
})
t.Run("ChainP", func(t *testing.T) {
var err error
oops.ChainP(&err, tc.Errs...)
require.Equal(t, tc.Expect, err)
})
})
}
})
}