Skip to content

Commit

Permalink
move all handlers from err2.go and add Handlers helper to handlers.go
Browse files Browse the repository at this point in the history
  • Loading branch information
lainio committed Feb 13, 2024
1 parent 6bbd852 commit b56f802
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 54 deletions.
54 changes: 0 additions & 54 deletions err2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package err2
import (
"errors"
"fmt"
"os"

"github.com/lainio/err2/internal/handler"
)
Expand Down Expand Up @@ -184,59 +183,6 @@ func Throwf(format string, args ...any) {
panic(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)
// }))
func Err(f func(err error)) func(error) error {
return func(err error) error {
f(err)
return err
}
}

type nullDev struct{}

func (nullDev) Write([]byte) (int, error) { return 0, nil }
Expand Down
76 changes: 76 additions & 0 deletions handlers.go
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
}
}
45 changes: 45 additions & 0 deletions handlers_test.go
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())
}
})
}
}

0 comments on commit b56f802

Please sign in to comment.