Skip to content

Commit

Permalink
refactor(errors): update ErrorHandler api
Browse files Browse the repository at this point in the history
now ErrorHandler is responsible for the error propagation
  • Loading branch information
alebabai committed May 9, 2024
1 parent f161a2d commit 061bcf8
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func (err Error) Unwrap() error {

// Handler is an interface for processing errors.
type ErrorHandler interface {
Handle(ctx context.Context, err error)
Handle(ctx context.Context, err error) error
}

// ErrorHandlerFunc is an adapter type that allows the use of ordinary functions as an [ErrorHandler].
type ErrorHandlerFunc func(ctx context.Context, err error)
type ErrorHandlerFunc func(ctx context.Context, err error) error

// Handle calls itself passing all arguments through.
func (fn ErrorHandlerFunc) Handle(ctx context.Context, err error) {
fn(ctx, err)
func (fn ErrorHandlerFunc) Handle(ctx context.Context, err error) error {
return fn(ctx, err)
}

// WrapErrorMiddleware returns a middleware that wraps errors with additional context using the Error type.
Expand All @@ -49,12 +49,14 @@ func WrapErrorMiddleware() Middleware {
}
}

// CatchErrorMiddleware returns a middleware that catches and handles errors without propagation.
// CatchErrorMiddleware returns a middleware that catches and handles errors with an [ErrorHandler].
func CatchErrorMiddleware(eh ErrorHandler) Middleware {
return func(next Handler) Handler {
return HandlerFunc(func(ctx context.Context, msg Message) error {
if err := next.Handle(ctx, msg); err != nil {
eh.Handle(ctx, err)
if err := eh.Handle(ctx, err); err != nil {
return err
}
}

return nil
Expand Down

0 comments on commit 061bcf8

Please sign in to comment.