Skip to content

Commit 8b6f2ea

Browse files
committed
new err2.Handle API called when err == nil, experimental!
1 parent b7661da commit 8b6f2ea

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

err2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func Handle(err *error, a ...any) {
7272
// how how it works with defer.
7373
r := recover()
7474

75-
if !handler.WorkToDo(r, err) {
75+
if !handler.WorkToDo(r, err) && !handler.NoerrCallToDo(a...) {
7676
return
7777
}
7878

err2_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ func TestTry_Error(t *testing.T) {
4949
t.Fail() // If everything works we are never here
5050
}
5151

52+
func TestHandle_NoError(t *testing.T) {
53+
var err error
54+
var handlerCalled bool
55+
defer func() {
56+
test.RequireEqual(t, handlerCalled, true)
57+
}()
58+
defer err2.Handle(&err, func(err error) error {
59+
// this should not be called, so lets try to fuckup things...
60+
handlerCalled = false
61+
return err
62+
})
63+
64+
// This is the handler we are thesting!
65+
defer err2.Handle(&err, func(noerr bool) {
66+
handlerCalled = noerr
67+
})
68+
69+
try.To(noErr())
70+
}
71+
5272
func TestPanickingCatchAll(t *testing.T) {
5373
type args struct {
5474
f func()

internal/handler/handler.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ type (
2020
PanicHandler = func(p any)
2121
ErrorHandler = func(err error) error // this is only proper type that work
2222
NilHandler = func(err error) error // these two are the same
23-
CheckHandler = func(noerr bool) error
23+
24+
//CheckHandler = func(noerr bool, err error) error
25+
CheckHandler = func(noerr bool)
2426
)
2527

2628
// Info tells to Process function how to proceed.
@@ -58,12 +60,19 @@ const (
5860
wrapError = ": %w"
5961
)
6062

61-
func PanicNoop(_ any) {}
63+
func PanicNoop(any) {}
6264
func NilNoop(err error) error { return err }
6365

6466
// func ErrorNoop(err error) {}
6567

6668
func (i *Info) callNilHandler() {
69+
if i.CheckHandler != nil {
70+
i.CheckHandler(true)
71+
// there is no err and user wants to handle OK with our pkg:
72+
// nothing more to do here after callNilHandler call
73+
return
74+
}
75+
6776
if i.safeErr() != nil {
6877
i.checkErrorTracer()
6978
}
@@ -208,6 +217,14 @@ func WorkToDo(r any, err *error) bool {
208217
return (err != nil && *err != nil) || r != nil
209218
}
210219

220+
func NoerrCallToDo(a ...any) (yes bool) {
221+
//var yes bool
222+
if len(a) != 0 {
223+
_, yes = a[0].(CheckHandler)
224+
}
225+
return yes
226+
}
227+
211228
// Process executes error handling logic. Panics and whole defer stack is
212229
// included.
213230
//

0 commit comments

Comments
 (0)