dont panic on nil ErrorHandler
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
af91220a09
commit
f28de46379
5
http.go
5
http.go
@ -2,6 +2,7 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -85,6 +86,10 @@ func (h *httpServer) NewHandler(handler interface{}, opts ...server.HandlerOptio
|
|||||||
sopts: h.opts,
|
sopts: h.opts,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hdlr.errorHandler = DefaultErrorHandler
|
||||||
|
if fn, ok := options.Context.Value(errorHandlerKey{}).(func(ctx context.Context, s server.Handler, w http.ResponseWriter, r *http.Request, err error, status int)); ok && fn != nil {
|
||||||
|
hdlr.errorHandler = fn
|
||||||
|
}
|
||||||
tp := reflect.TypeOf(handler)
|
tp := reflect.TypeOf(handler)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
12
options.go
12
options.go
@ -13,14 +13,17 @@ type rspCodeVal struct {
|
|||||||
code int
|
code int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetError pass error to caller
|
||||||
func SetError(err interface{}) error {
|
func SetError(err interface{}) error {
|
||||||
return &Error{err: err}
|
return &Error{err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error struct holds error
|
||||||
type Error struct {
|
type Error struct {
|
||||||
err interface{}
|
err interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Error func for error interface
|
||||||
func (err *Error) Error() string {
|
func (err *Error) Error() string {
|
||||||
return fmt.Sprintf("%v", err.err)
|
return fmt.Sprintf("%v", err.err)
|
||||||
}
|
}
|
||||||
@ -43,12 +46,21 @@ func GetRspCode(ctx context.Context) int {
|
|||||||
|
|
||||||
type middlewareKey struct{}
|
type middlewareKey struct{}
|
||||||
|
|
||||||
|
// Middleware passes http middlewares
|
||||||
func Middleware(mw ...func(http.Handler) http.Handler) server.Option {
|
func Middleware(mw ...func(http.Handler) http.Handler) server.Option {
|
||||||
return server.SetOption(middlewareKey{}, mw)
|
return server.SetOption(middlewareKey{}, mw)
|
||||||
}
|
}
|
||||||
|
|
||||||
type serverKey struct{}
|
type serverKey struct{}
|
||||||
|
|
||||||
|
// Server provide ability to pass *http.Server
|
||||||
func Server(hs *http.Server) server.Option {
|
func Server(hs *http.Server) server.Option {
|
||||||
return server.SetOption(serverKey{}, hs)
|
return server.SetOption(serverKey{}, hs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return server.SetOption(errorHandlerKey{}, fn)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user