Skip to content

Commit cf5f95f

Browse files
committed
all error handlers: func(err error) error
1 parent 0840a45 commit cf5f95f

File tree

5 files changed

+53
-40
lines changed

5 files changed

+53
-40
lines changed

err2_test.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestDefault_Error(t *testing.T) {
4242

4343
func TestTry_Error(t *testing.T) {
4444
var err error
45-
defer err2.Handle(&err, func() {})
45+
defer err2.Handle(&err, func(err error) error { return err })
4646

4747
try.To1(throw())
4848

@@ -61,7 +61,10 @@ func TestPanickingCatchAll(t *testing.T) {
6161
{"general panic",
6262
args{
6363
func() {
64-
defer err2.Catch(func(err error) {}, func(v any) {})
64+
defer err2.Catch(
65+
func(err error) error { return err },
66+
func(v any) {},
67+
)
6568
panic("panic")
6669
},
6770
},
@@ -70,7 +73,10 @@ func TestPanickingCatchAll(t *testing.T) {
7073
{"runtime.error panic",
7174
args{
7275
func() {
73-
defer err2.Catch(func(err error) {}, func(v any) {})
76+
defer err2.Catch(
77+
func(err error) error { return err },
78+
func(v any) {},
79+
)
7480
var b []byte
7581
b[0] = 0
7682
},
@@ -90,7 +96,7 @@ func TestPanickingCatchAll(t *testing.T) {
9096
{"stop panic with error handler in catch",
9197
args{
9298
func() {
93-
defer err2.Catch(func(err error) {})
99+
defer err2.Catch(func(err error) error { return err })
94100
var b []byte
95101
b[0] = 0
96102
},
@@ -121,7 +127,7 @@ func TestPanickingCarryOn_Handle(t *testing.T) {
121127
args{
122128
func() {
123129
var err error
124-
defer err2.Handle(&err, func() {})
130+
defer err2.Handle(&err, func(err error) error { return err })
125131
panic("panic")
126132
},
127133
},
@@ -131,7 +137,7 @@ func TestPanickingCarryOn_Handle(t *testing.T) {
131137
args{
132138
func() {
133139
var err error
134-
defer err2.Handle(&err, func() {})
140+
defer err2.Handle(&err, func(err error) error { return err })
135141
var b []byte
136142
b[0] = 0
137143
},
@@ -201,7 +207,7 @@ func TestPanicking_Handle(t *testing.T) {
201207
{"general panic plus err handler",
202208
args{
203209
func() (err error) {
204-
defer err2.Handle(&err, func() {})
210+
defer err2.Handle(&err, func(err error) error { return err })
205211
panic("panic")
206212
},
207213
},
@@ -210,7 +216,10 @@ func TestPanicking_Handle(t *testing.T) {
210216
{"general panic stoped with handler plus err handler",
211217
args{
212218
func() (err error) {
213-
defer err2.Handle(&err, func() {}, func(p any) {})
219+
defer err2.Handle(&err,
220+
func(err error) error { return err },
221+
func(p any) {},
222+
)
214223
panic("panic")
215224
},
216225
},
@@ -285,7 +294,7 @@ func TestPanicking_Catch(t *testing.T) {
285294
{"general panic",
286295
args{
287296
func() {
288-
defer err2.Catch(func(err error) {})
297+
defer err2.Catch(func(err error) error { return err })
289298
panic("panic")
290299
},
291300
},
@@ -294,7 +303,7 @@ func TestPanicking_Catch(t *testing.T) {
294303
{"runtime.error panic",
295304
args{
296305
func() {
297-
defer err2.Catch(func(err error) {})
306+
defer err2.Catch(func(err error) error { return err })
298307
var b []byte
299308
b[0] = 0
300309
},
@@ -321,9 +330,10 @@ func TestCatch_Error(t *testing.T) {
321330
}
322331

323332
func Test_TryOutError(t *testing.T) {
324-
defer err2.Catch(func(err error) {
333+
defer err2.Catch(func(err error) error {
325334
test.RequireEqual(t, err.Error(), "fails: test: this is an ERROR",
326335
"=> we should catch right error str here")
336+
return err
327337
})
328338

329339
var retVal string
@@ -347,9 +357,10 @@ func TestCatch_Panic(t *testing.T) {
347357
}()
348358

349359
defer err2.Catch(
350-
func(err error) {
360+
func(err error) error {
351361
t.Log("it was panic, not an error")
352362
t.Fail() // we should not be here
363+
return nil
353364
},
354365
func(v any) {
355366
panicHandled = true
@@ -457,26 +468,23 @@ func ExampleHandle_deferStack() {
457468

458469
func ExampleHandle_handlerFn() {
459470
doSomething := func(a, b int) (err error) {
460-
defer err2.Handle(&err, func() {
471+
defer err2.Handle(&err, func(err error) error {
461472
// Example for just annotating current err. Normally Handle is
462473
// used for cleanup. See CopyFile example for more information.
463-
err = fmt.Errorf("error with (%d, %d): %v", a, b, err)
474+
return fmt.Errorf("error with (%d, %d): %v", a, b, err)
464475
})
465476
try.To1(throw())
466477
return err
467478
}
468479
err := doSomething(1, 2)
469480
fmt.Printf("%v", err)
470-
// Output: this is an ERROR
481+
// Output: error with (1, 2): this is an ERROR
471482
}
472483

473-
// TODO:
474-
// Output: error with (1, 2): this is an ERROR
475-
476484
func ExampleHandle_noThrow() {
477485
doSomething := func(a, b int) (err error) {
478-
defer err2.Handle(&err, func() {
479-
err = fmt.Errorf("error with (%d, %d): %v", a, b, err)
486+
defer err2.Handle(&err, func(err error) error {
487+
return fmt.Errorf("error with (%d, %d): %v", a, b, err)
480488
})
481489
try.To1(noThrow())
482490
return err

internal/handler/handler.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type (
1919
// we want these to be type aliases, so they are much nicer to use
2020
PanicHandler = func(p any)
2121
ErrorHandler = func(err error) error // this is only proper type that work
22-
NilHandler = func() error // TODO: we remove this
22+
NilHandler = func(err error) error // these two are the same
2323
CheckHandler = func(noerr bool) error
2424
)
2525

@@ -58,8 +58,8 @@ const (
5858
wrapError = ": %w"
5959
)
6060

61-
func PanicNoop(_ any) {}
62-
func NilNoop() error { return nil }
61+
func PanicNoop(_ any) {}
62+
func NilNoop(err error) error { return err }
6363

6464
// func ErrorNoop(err error) {}
6565

@@ -68,7 +68,7 @@ func (i *Info) callNilHandler() {
6868
i.checkErrorTracer()
6969
}
7070
if i.NilHandler != nil {
71-
*i.Err = i.NilHandler()
71+
*i.Err = i.NilHandler(i.werr)
7272
} else {
7373
i.defaultNilHandler()
7474
}
@@ -160,7 +160,7 @@ func (i *Info) defaultNilHandler() {
160160

161161
func (i *Info) safeCallNilHandler() {
162162
if i.NilHandler != nil {
163-
*i.Err = i.NilHandler()
163+
*i.Err = i.NilHandler(i.werr)
164164
}
165165
}
166166

@@ -226,12 +226,12 @@ func Process(info *Info) {
226226
}
227227
}
228228

229-
func PreProcess(er *error, info *Info, a ...any) error {
229+
func PreProcess(errPtr *error, info *Info, a ...any) error {
230230
// Bug in Go?
231231
// start to use local error ptr only for optimization reasons.
232232
// We get 3x faster defer handlers without unsing ptr to original err
233233
// named return val. Reason is unknown.
234-
err := x.Whom(er != nil, *er, nil)
234+
err := x.Whom(errPtr != nil, *errPtr, nil)
235235
info.Err = &err
236236

237237
// We want the function who sets the handler, i.e. calls the
@@ -311,10 +311,11 @@ func processArg(info *Info, i int, a ...any) {
311311
info.Args = a[i+1:]
312312
case ErrorHandler: // err2.Catch uses this
313313
info.ErrorHandler = first
314+
info.NilHandler = first
314315
case PanicHandler: // err2.Catch uses this
315316
info.PanicHandler = first
316-
case NilHandler:
317-
info.NilHandler = first
317+
// case NilHandler:
318+
// info.NilHandler = first
318319
case CheckHandler:
319320
info.CheckHandler = first
320321
case nil:

internal/handler/handler_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ func TestProcess(t *testing.T) {
2727
args args
2828
want want
2929
}{
30-
// TODO: this test e.g. has problem because Process is not called
31-
// anymore if Any and Err are nil. Check is done before Process call.
32-
// - check with handler.WorkToDo() should we even test this.
3330
{"all nil and our handlers",
3431
args{Info: handler.Info{
3532
Any: nil,
@@ -285,10 +282,9 @@ func panicHandler(_ any) {
285282
panicHandlerCalled = true
286283
}
287284

288-
func nilHandlerForAnnotate() error {
285+
func nilHandlerForAnnotate(err error) error {
289286
nilHandlerCalled = true
290-
// in real case this is closure and it has access to err val
291-
myErrVal = fmt.Errorf("nil annotate: %v", "error")
287+
myErrVal = fmt.Errorf("nil annotate: %w", err)
292288
return myErrVal
293289
}
294290

@@ -303,7 +299,7 @@ func errorHandler(err error) error {
303299
return err
304300
}
305301

306-
func nilHandler() error {
302+
func nilHandler(err error) error {
307303
nilHandlerCalled = true
308-
return nil
304+
return err
309305
}

scripts/functions.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,16 @@ todo_assert() {
460460
ag 'assert\.[DP]+\.'
461461
}
462462

463+
todo_handle_func() {
464+
dlog "searching old error handlers"
465+
ag 'err2\.Handle\(&err, func\(\)'
466+
}
467+
468+
todo_catch_func() {
469+
dlog "searching old error handlers"
470+
ag 'err2\.Catch\(func\(err error\)'
471+
}
472+
463473
lint() {
464474
dlog "Linter check for missing defers"
465475
ag '^\s*err2\.(Handle|Catch)'

try/out_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ func TestResult2_Logf(t *testing.T) {
9393
err2.SetLogTracer(os.Stdout)
9494

9595
convTwoStr := func(s1, s2 string) (_ int, _ int, err error) {
96-
//defer err2.Handle(&err, nil) // TODO: why this is not working!!
97-
//TODO
98-
defer err2.Handle(&err)
96+
defer err2.Handle(&err, nil)
9997

10098
return try.To1(strconv.Atoi(s1)), try.To1(strconv.Atoi(s2)), nil
10199
}

0 commit comments

Comments
 (0)