Skip to content

Commit 71ea756

Browse files
committed
empty Catch default is logging now as try.Out().Logf()
1 parent f664e14 commit 71ea756

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

err2.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ func Handle(err *error, a ...any) {
103103
//
104104
// The deferred Catch is very convenient, because it makes your current
105105
// goroutine panic and error-safe, one line only! You can fine tune its
106-
// behavior with functions like err2.SetErrorTrace, assert.SetDefault, etc.
107-
// Start with the defaults.
106+
// behavior with functions like err2.SetErrorTrace, assert.SetDefault, and
107+
// logging settings. Start with the defaults and simplest version of Catch:
108108
//
109-
// defer err2.Catch()
109+
// defer err2.Catch() // writes errors to logs and panic traces to stderr
110110
//
111111
// Catch support logging as well:
112112
//
@@ -147,7 +147,7 @@ func Catch(a ...any) {
147147
err = handler.PreProcess(&err, &handler.Info{
148148
CallerName: "Catch",
149149
Any: r,
150-
NilHandler: handler.NilNoop,
150+
// NilHandler: handler.NilNoop, // TODO: why this is here?
151151
}, a...)
152152
doTrace(err)
153153
}

err2_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package err2_test
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
67
"os"
8+
"runtime"
79
"testing"
810

911
"github.com/lainio/err2"
@@ -898,6 +900,46 @@ func BenchmarkRecursionWithTryAnd_Empty_Defer(b *testing.B) {
898900
}
899901
}
900902

903+
func doWork(ePtr *error, r any) {
904+
switch v := r.(type) {
905+
case nil:
906+
return
907+
case runtime.Error:
908+
*ePtr = errors.Join(v, *ePtr)
909+
case error:
910+
*ePtr = errors.Join(v, *ePtr)
911+
default:
912+
// panicing
913+
}
914+
}
915+
916+
func BenchmarkRecursionWithTryAnd_HeavyPtrPtr_Defer(b *testing.B) {
917+
var recursion func(a int) (r int, err error)
918+
recursion = func(a int) (r int, err error) {
919+
defer func(ePtr *error) {
920+
r := recover()
921+
nothingToDo := r == nil && (ePtr == nil || *ePtr == nil)
922+
if nothingToDo {
923+
return
924+
}
925+
doWork(ePtr, r)
926+
}(&err)
927+
928+
if a == 0 {
929+
return 0, nil
930+
}
931+
s := try.To1(noThrow())
932+
_ = s
933+
r = try.To1(recursion(a - 1))
934+
r += a
935+
return r, nil
936+
}
937+
938+
for n := 0; n < b.N; n++ {
939+
_, _ = recursion(100)
940+
}
941+
}
942+
901943
func BenchmarkRecursionWithTryAndDefer(b *testing.B) {
902944
var recursion func(a int) (r int, err error)
903945
recursion = func(a int) (r int, err error) {

internal/handler/handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func PreProcess(errPtr *error, info *Info, a ...any) error {
267267
if len(a) > 0 {
268268
subProcess(info, a...)
269269
} else {
270-
fnName := "Handle"
270+
fnName := "Handle" // default
271271
if info.CallerName != "" {
272272
fnName = info.CallerName
273273
}
@@ -292,8 +292,7 @@ func PreProcess(errPtr *error, info *Info, a ...any) error {
292292

293293
Process(info)
294294

295-
logCatchCallMode := defCatchCallMode && firstArgIsString(a...)
296-
if curErr := info.safeErr(); logCatchCallMode && curErr != nil {
295+
if curErr := info.safeErr(); defCatchCallMode && curErr != nil {
297296
_, _, frame, ok := debug.FuncName(debug.StackInfo{
298297
PackageName: "",
299298
FuncName: "Catch",
@@ -308,7 +307,8 @@ func PreProcess(errPtr *error, info *Info, a ...any) error {
308307
return err
309308
}
310309

311-
func firstArgIsString(a ...any) bool {
310+
// firstArgIsString not used any more.
311+
func _(a ...any) bool {
312312
if len(a) > 0 {
313313
_, isStr := a[0].(string)
314314
return isStr

0 commit comments

Comments
 (0)