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 a2521bd
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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].
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 a2521bd

Please sign in to comment.