Skip to content

Commit 5894459

Browse files
committed
new error handler signature docs & handler helpers: Noop,..
1 parent c17cc2b commit 5894459

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

err2.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,16 @@ var (
5555
// In case of the actual error handling, the handler function should be given as
5656
// an second argument:
5757
//
58-
// defer err2.Handle(&err, func() {
58+
// defer err2.Handle(&err, func(err error) error {
5959
// os.Remove(dst)
60+
// return err
6061
// })
6162
//
6263
// If you need to stop general panics in handler, you can do that by giving a
6364
// panic handler function:
6465
//
6566
// defer err2.Handle(&err,
66-
// func() {
67-
// os.Remove(dst)
68-
// },
67+
// err2.Err( func(error) { os.Remove(dst) }), // err2.Err keeps it short
6968
// func(p any) {} // panic handler, it's stops panics, you can re-throw
7069
// )
7170
func Handle(err *error, a ...any) {
@@ -109,16 +108,23 @@ func Handle(err *error, a ...any) {
109108
// currently set log.
110109
//
111110
// The next one stops errors and panics, but allows you handle errors, like
112-
// cleanups, etc. The output results depends on the current Tracer and assert
111+
// cleanups, etc. The error handler function has same signature as Handle's
112+
// error handling function: func(err error) error. By returning nil resets the
113+
// error, which allows e.g. prevent automatic error logs to happening.
114+
// Otherwise, the output results depends on the current Tracer and assert
113115
// settings. Default setting print call stacks for panics but not for errors.
114116
//
115-
// defer err2.Catch(func(err error) {})
117+
// defer err2.Catch(func(err error) error { return err} )
118+
//
119+
// or if you you prefer to use dedicated helpers:
120+
//
121+
// defer err2.Catch(err2.Reset)
116122
//
117123
// The last one calls your error handler, and you have an explicit panic
118124
// handler too, where you can e.g. continue panicking to propagate it for above
119125
// callers:
120126
//
121-
// defer err2.Catch(func(err error) {}, func(p any) {})
127+
// defer err2.Catch(func(err error) error { return err }, func(p any) {})
122128
func Catch(a ...any) {
123129
// This and others are similar but we need to call `recover` here because
124130
// how it works with defer.
@@ -159,6 +165,27 @@ func Throwf(format string, args ...any) {
159165
panic(err)
160166
}
161167

168+
// Noop is predeclared helper to use with Handle and Catch. It keeps the current
169+
// error value the same. You can use it like this:
170+
//
171+
// defer err2.Handle(&err, err2.Noop)
172+
func Noop(err error) error { return err }
173+
174+
// Reset is predeclared helper to use with Handle and Catch. It sets the current
175+
// error value to nil. You can use it like this to reset the error:
176+
//
177+
// defer err2.Handle(&err, err2.Reset)
178+
func Reset(error) error { return nil }
179+
180+
// Err is predeclared helper to use with Handle and Catch. It offers simplifier
181+
// for error handling function.
182+
func Err(f func(err error)) func(error) error {
183+
return func(err error) error {
184+
f(err)
185+
return err
186+
}
187+
}
188+
162189
func doTrace(err error) {
163190
if err == nil || err.Error() == "" {
164191
return

err2_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestPanickingCatchAll(t *testing.T) {
6262
args{
6363
func() {
6464
defer err2.Catch(
65-
func(err error) error { return err },
65+
err2.Noop,
6666
func(v any) {},
6767
)
6868
panic("panic")
@@ -74,7 +74,7 @@ func TestPanickingCatchAll(t *testing.T) {
7474
args{
7575
func() {
7676
defer err2.Catch(
77-
func(err error) error { return err },
77+
err2.Err(func(error) {}), // Using simplifier
7878
func(v any) {},
7979
)
8080
var b []byte
@@ -96,7 +96,7 @@ func TestPanickingCatchAll(t *testing.T) {
9696
{"stop panic with error handler in catch",
9797
args{
9898
func() {
99-
defer err2.Catch(func(err error) error { return err })
99+
defer err2.Catch(err2.Noop)
100100
var b []byte
101101
b[0] = 0
102102
},
@@ -127,7 +127,7 @@ func TestPanickingCarryOn_Handle(t *testing.T) {
127127
args{
128128
func() {
129129
var err error
130-
defer err2.Handle(&err, func(err error) error { return err })
130+
defer err2.Handle(&err, err2.Noop)
131131
panic("panic")
132132
},
133133
},
@@ -137,7 +137,7 @@ func TestPanickingCarryOn_Handle(t *testing.T) {
137137
args{
138138
func() {
139139
var err error
140-
defer err2.Handle(&err, func(err error) error { return err })
140+
defer err2.Handle(&err, err2.Noop)
141141
var b []byte
142142
b[0] = 0
143143
},
@@ -207,7 +207,7 @@ func TestPanicking_Handle(t *testing.T) {
207207
{"general panic plus err handler",
208208
args{
209209
func() (err error) {
210-
defer err2.Handle(&err, func(err error) error { return err })
210+
defer err2.Handle(&err, err2.Noop)
211211
panic("panic")
212212
},
213213
},
@@ -294,7 +294,7 @@ func TestPanicking_Catch(t *testing.T) {
294294
{"general panic",
295295
args{
296296
func() {
297-
defer err2.Catch(func(err error) error { return err })
297+
defer err2.Catch(err2.Noop)
298298
panic("panic")
299299
},
300300
},
@@ -303,7 +303,7 @@ func TestPanicking_Catch(t *testing.T) {
303303
{"runtime.error panic",
304304
args{
305305
func() {
306-
defer err2.Catch(func(err error) error { return err })
306+
defer err2.Catch(err2.Noop)
307307
var b []byte
308308
b[0] = 0
309309
},

0 commit comments

Comments
 (0)