File tree 3 files changed +40
-3
lines changed
3 files changed +40
-3
lines changed Original file line number Diff line number Diff line change @@ -72,7 +72,7 @@ func Handle(err *error, a ...any) {
72
72
// how how it works with defer.
73
73
r := recover ()
74
74
75
- if ! handler .WorkToDo (r , err ) {
75
+ if ! handler .WorkToDo (r , err ) && ! handler . NoerrCallToDo ( a ... ) {
76
76
return
77
77
}
78
78
Original file line number Diff line number Diff line change @@ -49,6 +49,26 @@ func TestTry_Error(t *testing.T) {
49
49
t .Fail () // If everything works we are never here
50
50
}
51
51
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
+
52
72
func TestPanickingCatchAll (t * testing.T ) {
53
73
type args struct {
54
74
f func ()
Original file line number Diff line number Diff line change 20
20
PanicHandler = func (p any )
21
21
ErrorHandler = func (err error ) error // this is only proper type that work
22
22
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 )
24
26
)
25
27
26
28
// Info tells to Process function how to proceed.
@@ -58,12 +60,19 @@ const (
58
60
wrapError = ": %w"
59
61
)
60
62
61
- func PanicNoop (_ any ) {}
63
+ func PanicNoop (any ) {}
62
64
func NilNoop (err error ) error { return err }
63
65
64
66
// func ErrorNoop(err error) {}
65
67
66
68
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
+
67
76
if i .safeErr () != nil {
68
77
i .checkErrorTracer ()
69
78
}
@@ -208,6 +217,14 @@ func WorkToDo(r any, err *error) bool {
208
217
return (err != nil && * err != nil ) || r != nil
209
218
}
210
219
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
+
211
228
// Process executes error handling logic. Panics and whole defer stack is
212
229
// included.
213
230
//
You can’t perform that action at this time.
0 commit comments