Documentation ¶
Index ¶
- func DefaultMapping() map[simplerr.Code]HTTPStatus
- func NewHandlerFuncAdapter(h HandlerFunc, opts ...HandlerOption) http.HandlerFunc
- func SetDefaultErrorStatus(code int)
- func SetMapping(m map[simplerr.Code]HTTPStatus)
- type ErrorHandler
- type HTTPStatus
- type Handler
- type HandlerAdapter
- type HandlerFunc
- type HandlerOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultMapping ¶
func DefaultMapping() map[simplerr.Code]HTTPStatus
DefaultMapping returns the default mapping of SimpleError codes to HTTP status codes
func NewHandlerFuncAdapter ¶ added in v0.1.8
func NewHandlerFuncAdapter(h HandlerFunc, opts ...HandlerOption) http.HandlerFunc
NewHandlerFuncAdapter returns a http.HandlerFunc which calls SetStatus on the returned error
func SetDefaultErrorStatus ¶ added in v0.1.10
func SetDefaultErrorStatus(code int)
SetDefaultErrorStatus changes the default HTTP status code for when a translation could not be found. The default status code is 500.
func SetMapping ¶
func SetMapping(m map[simplerr.Code]HTTPStatus)
SetMapping sets the mapping from simplerr.Code to HTTP status code
Types ¶
type ErrorHandler ¶ added in v0.1.10
type ErrorHandler func(http.ResponseWriter, *http.Request, error)
ErrorHandler is an error handling function for HTTP handlers
var DefaultErrorHandler ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) { SetStatus(w, err) }
DefaultErrorHandler is the default error handling function for HTTP handlers, it Sets the response status based on the handler's returned error using the SimpleError to HTTP mapping. The DefaultErrorHandler can be changed to also provide other error handling such as logging.
type HTTPStatus ¶
type HTTPStatus = int
HTTPStatus is the HTTP status code
func GetStatus ¶ added in v0.1.10
func GetStatus(err error) (status HTTPStatus, found bool)
GetStatus returns the HTTP status that the error maps to if the provided error is a SimpleError. If a mapping could not be found or the error is nil, then the boolean second argument is returned as false
func SetStatus ¶
func SetStatus(r http.ResponseWriter, err error) HTTPStatus
SetStatus sets the http.Response status from the error code in the provided error. It returns the HTTPStatus that was written If the error contains a SimpleError, then the status is determined by the mapping. If the error is not a SimpleError then the default error status code will be set. If the error is nil, then no status will be set.
type Handler ¶ added in v0.1.8
type Handler interface {
ServeHTTP(http.ResponseWriter, *http.Request) error
}
Handler is analogous to http.Handler but returns an error
type HandlerAdapter ¶ added in v0.1.8
type HandlerAdapter struct {
// contains filtered or unexported fields
}
HandlerAdapter adapts the Handler interface to the http.Handler interface
func NewHandlerAdapter ¶ added in v0.1.8
func NewHandlerAdapter(h Handler, opts ...HandlerOption) *HandlerAdapter
NewHandlerAdapter returns a HandlerAdapter that can be used with the standard library http package.
func (HandlerAdapter) ServeHTTP ¶ added in v0.1.8
func (h HandlerAdapter) ServeHTTP(writer http.ResponseWriter, request *http.Request)
ServeHTTP calls the underlying handler's ServeHTTP method and calls SetStatus on the returned error
type HandlerFunc ¶ added in v0.1.8
type HandlerFunc func(http.ResponseWriter, *http.Request) error
HandlerFunc is analogous to http.HandlerFunc but returns an error
func (HandlerFunc) Adapter ¶ added in v0.1.8
func (h HandlerFunc) Adapter(opts ...HandlerOption) http.HandlerFunc
Adapter returns a http.HandlerFunc which calls SetStatus on the returned error
func (HandlerFunc) ServeHTTP ¶ added in v0.1.10
func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) error
ServerHTTP implements the Handler interface
type HandlerOption ¶ added in v0.1.10
type HandlerOption func(adapter *HandlerAdapter)
HandlerOption are options to change the behaviour of the HandlerAdapter
func WithErrorHandler ¶ added in v0.1.10
func WithErrorHandler(h ErrorHandler) HandlerOption
WithErrorHandler is an HandlerOption to change the error handling function