@@ -11,8 +11,10 @@ import (
11
11
"github.com/lainio/err2/try"
12
12
)
13
13
14
+ const errStringInThrow = "this is an ERROR"
15
+
14
16
func throw () (string , error ) {
15
- return "" , fmt .Errorf ("this is an ERROR" )
17
+ return "" , fmt .Errorf (errStringInThrow )
16
18
}
17
19
18
20
func twoStrNoThrow () (string , string , error ) { return "test" , "test" , nil }
@@ -113,12 +115,16 @@ func TestHandle_noerrHandler(t *testing.T) {
113
115
try .To1 (throw ())
114
116
})
115
117
116
- t .Run ("noerr is first and error happens" , func (t * testing.T ) {
118
+ t .Run ("noerr is first and error happens with many handlers " , func (t * testing.T ) {
117
119
t .Parallel ()
118
- var err error
119
- var handlerCalled bool
120
+ var (
121
+ err error
122
+ finalAnnotatedErr = fmt .Errorf ("err: %v" , errStringInThrow )
123
+ handlerCalled bool
124
+ )
120
125
defer func () {
121
126
test .Require (t , ! handlerCalled )
127
+ test .RequireEqual (t , err .Error (), finalAnnotatedErr .Error ())
122
128
}()
123
129
124
130
// This is the handler we are thesting!
@@ -127,11 +133,18 @@ func TestHandle_noerrHandler(t *testing.T) {
127
133
handlerCalled = noerr
128
134
})
129
135
136
+ // important! test that our handler doesn't change the current error
137
+ // and it's not nil
138
+ defer err2 .Handle (& err , func (er error ) error {
139
+ test .Require (t , er != nil , "er val: " , er , err )
140
+ return er
141
+ })
142
+
130
143
defer err2 .Handle (& err , func (err error ) error {
131
144
// this should not be called, so lets try to fuckup things...
132
145
handlerCalled = false
133
146
test .Require (t , err != nil )
134
- return err
147
+ return finalAnnotatedErr
135
148
})
136
149
try .To1 (throw ())
137
150
})
@@ -191,7 +204,7 @@ func TestHandle_noerrHandler(t *testing.T) {
191
204
try .To (noErr ())
192
205
})
193
206
194
- t .Run ("noerr handler is first of MANY and error happens UNTIL reset " , func (t * testing.T ) {
207
+ t .Run ("noerr handler is first of MANY and error happens UNTIL RESET " , func (t * testing.T ) {
195
208
t .Parallel ()
196
209
var err error
197
210
var noerrHandlerCalled , errHandlerCalled bool
@@ -202,14 +215,14 @@ func TestHandle_noerrHandler(t *testing.T) {
202
215
203
216
// This is the handler we are thesting!
204
217
defer err2 .Handle (& err , func (noerr bool ) {
205
- test .Require (t , true )
218
+ test .Require (t , true ) // we are here, for debugging
206
219
test .Require (t , noerr )
207
220
noerrHandlerCalled = noerr
208
221
})
209
222
210
- // this is the err handler that resets the error to nil
223
+ // this is the err handler that -- RESETS -- the error to nil
211
224
defer err2 .Handle (& err , func (err error ) error {
212
- test .Require (t , true ) // helps fast debugging
225
+ test .Require (t , err != nil ) // helps fast debugging
213
226
214
227
// this should not be called, so lets try to fuckup things...
215
228
noerrHandlerCalled = false // see first deferred function
@@ -219,7 +232,7 @@ func TestHandle_noerrHandler(t *testing.T) {
219
232
})
220
233
221
234
defer err2 .Handle (& err , func (err error ) error {
222
- test .Require (t , true ) // helps fast debugging
235
+ test .Require (t , err != nil ) // helps fast debugging
223
236
// this should not be called, so lets try to fuckup things...
224
237
noerrHandlerCalled = false // see first deferred function
225
238
@@ -237,6 +250,7 @@ func TestHandle_noerrHandler(t *testing.T) {
237
250
test .Require (t , handlerCalled )
238
251
}()
239
252
253
+ defer err2 .Handle (& err )
240
254
defer err2 .Handle (& err )
241
255
242
256
defer err2 .Handle (& err , func (err error ) error {
0 commit comments