diff --git a/dispatcher.go b/dispatcher.go index 1a8a2e0..0bca41c 100644 --- a/dispatcher.go +++ b/dispatcher.go @@ -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} diff --git a/middleware/request/error_handler.go b/middleware/request/error_handler.go new file mode 100644 index 0000000..f39b3ff --- /dev/null +++ b/middleware/request/error_handler.go @@ -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 + }) + } +}