-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move all handlers from err2.go and add Handlers helper to handlers.go
- Loading branch information
Showing
3 changed files
with
121 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package err2 | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// Handlers is a helper to call several error handlers in a sequence. | ||
// | ||
// defer err2.Handle(&err, err2.Handlers(err2.Log, MapToHTTPErr)) | ||
func Handlers(f ...Handler) Handler { | ||
return func(err error) error { | ||
for _, handler := range f { | ||
err = handler(err) | ||
} | ||
return err | ||
} | ||
} | ||
|
||
// Stderr is a built-in helper to use with Handle and Catch. It prints the | ||
// error to stderr and it resets the current error value. It's a handy Catch | ||
// handler in main function. | ||
// | ||
// You can use it like this: | ||
// | ||
// func main() { | ||
// defer err2.Catch(err2.Stderr) | ||
func Stderr(err error) error { | ||
fmt.Fprintln(os.Stderr, err.Error()) | ||
return nil | ||
} | ||
|
||
// Stdout is a built-in helper to use with Handle and Catch. It prints the | ||
// error to stdout and it resets the current error value. It's a handy Catch | ||
// handler in main function. | ||
// | ||
// You can use it like this: | ||
// | ||
// func main() { | ||
// defer err2.Catch(err2.Stdout) | ||
func Stdout(err error) error { | ||
fmt.Fprintln(os.Stdout, err.Error()) | ||
return nil | ||
} | ||
|
||
// Noop is a built-in helper to use with Handle and Catch. It keeps the current | ||
// error value the same. You can use it like this: | ||
// | ||
// defer err2.Handle(&err, err2.Noop) | ||
func Noop(err error) error { return err } | ||
|
||
// Reset is a built-in helper to use with Handle and Catch. It sets the current | ||
// error value to nil. You can use it like this to reset the error: | ||
// | ||
// defer err2.Handle(&err, err2.Reset) | ||
func Reset(error) error { return nil } | ||
|
||
// Err is a built-in helper to use with Handle and Catch. It offers simplifier | ||
// for error handling function for cases where you don't need to change the | ||
// current error value. For instance, if you want to just write error to stdout, | ||
// and don't want to use SetLogTracer and keep it to write to your logs. | ||
// | ||
// defer err2.Catch(err2.Err(func(err error) { | ||
// fmt.Println("ERROR:", err) | ||
// })) | ||
// | ||
// Note, that since Err helper we have other helpers like Stdout that allows | ||
// previous block be written as simple as: | ||
// | ||
// defer err2.Catch(err2.Stdout) | ||
func Err(f func(err error)) Handler { | ||
return func(err error) error { | ||
f(err) | ||
return err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package err2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/lainio/err2/internal/test" | ||
) | ||
|
||
func TestHandlers(t *testing.T) { | ||
t.Parallel() | ||
type args struct { | ||
f []Handler | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want error | ||
}{ | ||
{"one", args{f: []Handler{Noop}}, ErrNotFound}, | ||
{"two", args{f: []Handler{Noop, Noop}}, ErrNotFound}, | ||
{"three", args{f: []Handler{Noop, Noop, Noop}}, ErrNotFound}, | ||
{"reset", args{f: []Handler{Noop, Noop, Reset}}, nil}, | ||
{"reset first", args{f: []Handler{Reset, Noop, Noop}}, nil}, | ||
{"reset second", args{f: []Handler{Noop, Reset, Noop}}, nil}, | ||
{"set new first", args{f: []Handler{ | ||
func(error) error { return ErrAlreadyExist }, Noop}}, ErrAlreadyExist}, | ||
{"set new second", args{f: []Handler{Noop, | ||
func(error) error { return ErrAlreadyExist }, Noop}}, ErrAlreadyExist}, | ||
{"set new first and reset", args{f: []Handler{ | ||
func(error) error { return ErrAlreadyExist }, Reset}}, nil}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
errHandler := Handlers(tt.args.f...) | ||
err := errHandler(ErrNotFound) | ||
if err == nil { | ||
test.Require(t, tt.want == nil) | ||
} else { | ||
test.RequireEqual(t, err.Error(), tt.want.Error()) | ||
} | ||
}) | ||
} | ||
} |