Skip to content

Commit b56f802

Browse files
committed
move all handlers from err2.go and add Handlers helper to handlers.go
1 parent 6bbd852 commit b56f802

File tree

3 files changed

+121
-54
lines changed

3 files changed

+121
-54
lines changed

err2.go

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package err2
33
import (
44
"errors"
55
"fmt"
6-
"os"
76

87
"github.com/lainio/err2/internal/handler"
98
)
@@ -184,59 +183,6 @@ func Throwf(format string, args ...any) {
184183
panic(err)
185184
}
186185

187-
// Stderr is a built-in helper to use with Handle and Catch. It prints the
188-
// error to stderr and it resets the current error value. It's a handy Catch
189-
// handler in main function.
190-
//
191-
// You can use it like this:
192-
//
193-
// func main() {
194-
// defer err2.Catch(err2.Stderr)
195-
func Stderr(err error) error {
196-
fmt.Fprintln(os.Stderr, err.Error())
197-
return nil
198-
}
199-
200-
// Stdout is a built-in helper to use with Handle and Catch. It prints the
201-
// error to stdout and it resets the current error value. It's a handy Catch
202-
// handler in main function.
203-
//
204-
// You can use it like this:
205-
//
206-
// func main() {
207-
// defer err2.Catch(err2.Stdout)
208-
func Stdout(err error) error {
209-
fmt.Fprintln(os.Stdout, err.Error())
210-
return nil
211-
}
212-
213-
// Noop is a built-in helper to use with Handle and Catch. It keeps the current
214-
// error value the same. You can use it like this:
215-
//
216-
// defer err2.Handle(&err, err2.Noop)
217-
func Noop(err error) error { return err }
218-
219-
// Reset is a built-in helper to use with Handle and Catch. It sets the current
220-
// error value to nil. You can use it like this to reset the error:
221-
//
222-
// defer err2.Handle(&err, err2.Reset)
223-
func Reset(error) error { return nil }
224-
225-
// Err is a built-in helper to use with Handle and Catch. It offers simplifier
226-
// for error handling function for cases where you don't need to change the
227-
// current error value. For instance, if you want to just write error to stdout,
228-
// and don't want to use SetLogTracer and keep it to write to your logs.
229-
//
230-
// defer err2.Catch(err2.Err(func(err error) {
231-
// fmt.Println("ERROR:", err)
232-
// }))
233-
func Err(f func(err error)) func(error) error {
234-
return func(err error) error {
235-
f(err)
236-
return err
237-
}
238-
}
239-
240186
type nullDev struct{}
241187

242188
func (nullDev) Write([]byte) (int, error) { return 0, nil }

handlers.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package err2
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
// Handlers is a helper to call several error handlers in a sequence.
9+
//
10+
// defer err2.Handle(&err, err2.Handlers(err2.Log, MapToHTTPErr))
11+
func Handlers(f ...Handler) Handler {
12+
return func(err error) error {
13+
for _, handler := range f {
14+
err = handler(err)
15+
}
16+
return err
17+
}
18+
}
19+
20+
// Stderr is a built-in helper to use with Handle and Catch. It prints the
21+
// error to stderr and it resets the current error value. It's a handy Catch
22+
// handler in main function.
23+
//
24+
// You can use it like this:
25+
//
26+
// func main() {
27+
// defer err2.Catch(err2.Stderr)
28+
func Stderr(err error) error {
29+
fmt.Fprintln(os.Stderr, err.Error())
30+
return nil
31+
}
32+
33+
// Stdout is a built-in helper to use with Handle and Catch. It prints the
34+
// error to stdout and it resets the current error value. It's a handy Catch
35+
// handler in main function.
36+
//
37+
// You can use it like this:
38+
//
39+
// func main() {
40+
// defer err2.Catch(err2.Stdout)
41+
func Stdout(err error) error {
42+
fmt.Fprintln(os.Stdout, err.Error())
43+
return nil
44+
}
45+
46+
// Noop is a built-in helper to use with Handle and Catch. It keeps the current
47+
// error value the same. You can use it like this:
48+
//
49+
// defer err2.Handle(&err, err2.Noop)
50+
func Noop(err error) error { return err }
51+
52+
// Reset is a built-in helper to use with Handle and Catch. It sets the current
53+
// error value to nil. You can use it like this to reset the error:
54+
//
55+
// defer err2.Handle(&err, err2.Reset)
56+
func Reset(error) error { return nil }
57+
58+
// Err is a built-in helper to use with Handle and Catch. It offers simplifier
59+
// for error handling function for cases where you don't need to change the
60+
// current error value. For instance, if you want to just write error to stdout,
61+
// and don't want to use SetLogTracer and keep it to write to your logs.
62+
//
63+
// defer err2.Catch(err2.Err(func(err error) {
64+
// fmt.Println("ERROR:", err)
65+
// }))
66+
//
67+
// Note, that since Err helper we have other helpers like Stdout that allows
68+
// previous block be written as simple as:
69+
//
70+
// defer err2.Catch(err2.Stdout)
71+
func Err(f func(err error)) Handler {
72+
return func(err error) error {
73+
f(err)
74+
return err
75+
}
76+
}

handlers_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package err2
2+
3+
import (
4+
"testing"
5+
6+
"github.com/lainio/err2/internal/test"
7+
)
8+
9+
func TestHandlers(t *testing.T) {
10+
t.Parallel()
11+
type args struct {
12+
f []Handler
13+
}
14+
tests := []struct {
15+
name string
16+
args args
17+
want error
18+
}{
19+
{"one", args{f: []Handler{Noop}}, ErrNotFound},
20+
{"two", args{f: []Handler{Noop, Noop}}, ErrNotFound},
21+
{"three", args{f: []Handler{Noop, Noop, Noop}}, ErrNotFound},
22+
{"reset", args{f: []Handler{Noop, Noop, Reset}}, nil},
23+
{"reset first", args{f: []Handler{Reset, Noop, Noop}}, nil},
24+
{"reset second", args{f: []Handler{Noop, Reset, Noop}}, nil},
25+
{"set new first", args{f: []Handler{
26+
func(error) error { return ErrAlreadyExist }, Noop}}, ErrAlreadyExist},
27+
{"set new second", args{f: []Handler{Noop,
28+
func(error) error { return ErrAlreadyExist }, Noop}}, ErrAlreadyExist},
29+
{"set new first and reset", args{f: []Handler{
30+
func(error) error { return ErrAlreadyExist }, Reset}}, nil},
31+
}
32+
for _, tt := range tests {
33+
tt := tt
34+
t.Run(tt.name, func(t *testing.T) {
35+
t.Parallel()
36+
errHandler := Handlers(tt.args.f...)
37+
err := errHandler(ErrNotFound)
38+
if err == nil {
39+
test.Require(t, tt.want == nil)
40+
} else {
41+
test.RequireEqual(t, err.Error(), tt.want.Error())
42+
}
43+
})
44+
}
45+
}

0 commit comments

Comments
 (0)