From 7c15ee784a10dcc781278704d346e7cde3dd5dff Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 23 Jan 2023 09:23:38 +0300 Subject: [PATCH] fix error handler Signed-off-by: Vasiliy Tolstov --- http.go | 6 +++++- options.go | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/http.go b/http.go index 84c8678..887a660 100644 --- a/http.go +++ b/http.go @@ -602,11 +602,15 @@ func (h *httpServer) Name() string { func NewServer(opts ...server.Option) *httpServer { options := server.NewOptions(opts...) + eh := DefaultErrorHandler + if v, ok := options.Context.Value(errorHandlerKey{}).(errorHandler); ok && v != nil { + eh = v + } return &httpServer{ opts: options, exit: make(chan chan error), subscribers: make(map[*httpSubscriber][]broker.Subscriber), - errorHandler: DefaultErrorHandler, + errorHandler: eh, pathHandlers: rhttp.NewTrie(), } } diff --git a/options.go b/options.go index 7a2903d..32fdc7b 100644 --- a/options.go +++ b/options.go @@ -68,10 +68,12 @@ func Server(hs *http.Server) server.Option { return server.SetOption(serverKey{}, hs) } +type errorHandler func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int) + type errorHandlerKey struct{} // ErrorHandler specifies handler for errors -func ErrorHandler(fn func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int)) server.Option { +func ErrorHandler(fn errorHandler) server.Option { return server.SetOption(errorHandlerKey{}, fn) } -- 2.45.2