@@ -55,17 +55,16 @@ var (
55
55
// In case of the actual error handling, the handler function should be given as
56
56
// an second argument:
57
57
//
58
- // defer err2.Handle(&err, func() {
58
+ // defer err2.Handle(&err, func(err error) error {
59
59
// os.Remove(dst)
60
+ // return err
60
61
// })
61
62
//
62
63
// If you need to stop general panics in handler, you can do that by giving a
63
64
// panic handler function:
64
65
//
65
66
// defer err2.Handle(&err,
66
- // func() {
67
- // os.Remove(dst)
68
- // },
67
+ // err2.Err( func(error) { os.Remove(dst) }), // err2.Err keeps it short
69
68
// func(p any) {} // panic handler, it's stops panics, you can re-throw
70
69
// )
71
70
func Handle (err * error , a ... any ) {
@@ -109,16 +108,23 @@ func Handle(err *error, a ...any) {
109
108
// currently set log.
110
109
//
111
110
// 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
113
115
// settings. Default setting print call stacks for panics but not for errors.
114
116
//
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)
116
122
//
117
123
// The last one calls your error handler, and you have an explicit panic
118
124
// handler too, where you can e.g. continue panicking to propagate it for above
119
125
// callers:
120
126
//
121
- // defer err2.Catch(func(err error) { }, func(p any) {})
127
+ // defer err2.Catch(func(err error) error { return err }, func(p any) {})
122
128
func Catch (a ... any ) {
123
129
// This and others are similar but we need to call `recover` here because
124
130
// how it works with defer.
@@ -159,6 +165,27 @@ func Throwf(format string, args ...any) {
159
165
panic (err )
160
166
}
161
167
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
+
162
189
func doTrace (err error ) {
163
190
if err == nil || err .Error () == "" {
164
191
return
0 commit comments