Skip to content

Commit

Permalink
Adds error handling midleware
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Apr 6, 2024
1 parent 8f6cf37 commit 44529f2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ type RequestHandler interface {
// RequestMiddlewere is interface for request middleweres
type RequestMiddlewere func(next RequestHandler) RequestHandler

// RequestHandlerFunc is a function that implements RequestHandler interface
type RequestHandlerFunc func(conn Connection, req Request) error

// Handle implements RequestHandler interface
func (f RequestHandlerFunc) Handle(conn Connection, req Request) error {
return f(conn, req)
}

// NewPipeDispatcher creates new instance of PipeDispatcher
func NewPipeDispatcher(backend Backend) *PipeDispatcher {
return &PipeDispatcher{backend: backend}
Expand Down
19 changes: 19 additions & 0 deletions middleware/request/error_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package request

import "github.com/ksysoev/wasabi"

type ErrorHandler func(conn wasabi.Connection, req wasabi.Request, err error) error

func NewErrorHandlingMiddleware(onError ErrorHandler) func(next wasabi.RequestHandler) wasabi.RequestHandler {
return func(next wasabi.RequestHandler) wasabi.RequestHandler {
return wasabi.RequestHandlerFunc(func(conn wasabi.Connection, req wasabi.Request) error {
err := next.Handle(conn, req)

if err != nil {
return onError(conn, req, err)
}

return nil
})
}
}

0 comments on commit 44529f2

Please sign in to comment.