Documentation ¶
Overview ¶
Package httperror is for writing HTTP handlers that return errors instead of handling them directly. See the documentation at https://github.com/johnwarden/httperror
Index ¶
- Variables
- func DefaultErrorHandler(w http.ResponseWriter, e error)
- func Errorf(s int, format string, args ...interface{}) error
- func New(s int, m string) error
- func NewPublic(status int, message string) error
- func PublicErrorf(status int, format string, args ...interface{}) error
- func PublicMessage(err error) string
- func StatusCode(err error) int
- func Wrap(err error, status int) error
- func WrapHandlerFunc(h func(w http.ResponseWriter, r *http.Request) error, eh ErrorHandler) http.HandlerFunc
- func WrapXHandlerFunc[P any](h func(w http.ResponseWriter, r *http.Request, p P) error, eh ErrorHandler) func(w http.ResponseWriter, r *http.Request, p P)
- func WriteResponse(w http.ResponseWriter, s int, m []byte)
- type ErrorHandler
- type Handler
- type HandlerFunc
- type Public
- type StandardMiddleware
- type XHandler
- type XHandlerFunc
Constants ¶
This section is empty.
Variables ¶
var BadGateway = httpError{http.StatusBadGateway}
BadGateway represents the StatusBadGateway HTTP error.
var BadRequest = httpError{http.StatusBadRequest}
BadRequest represents the StatusBadRequest HTTP error.
var Conflict = httpError{http.StatusConflict}
Conflict represents the StatusConflict HTTP error.
var ExpectationFailed = httpError{http.StatusExpectationFailed}
ExpectationFailed represents the StatusExpectationFailed HTTP error.
var FailedDependency = httpError{http.StatusFailedDependency}
FailedDependency represents the StatusFailedDependency HTTP error.
var Forbidden = httpError{http.StatusForbidden}
Forbidden represents the StatusForbidden HTTP error.
var GatewayTimeout = httpError{http.StatusGatewayTimeout}
GatewayTimeout represents the StatusGatewayTimeout HTTP error.
var Gone = httpError{http.StatusGone}
Gone represents the StatusGone HTTP error.
var HTTPVersionNotSupported = httpError{http.StatusHTTPVersionNotSupported}
HTTPVersionNotSupported represents the StatusHTTPVersionNotSupported HTTP error.
var InsufficientStorage = httpError{http.StatusInsufficientStorage}
InsufficientStorage represents the StatusInsufficientStorage HTTP error.
var InternalServerError = httpError{http.StatusInternalServerError}
InternalServerError represents the StatusInternalServerError HTTP error.
var LengthRequired = httpError{http.StatusLengthRequired}
LengthRequired represents the StatusLengthRequired HTTP error.
var Locked = httpError{http.StatusLocked}
Locked represents the StatusLocked HTTP error.
var LoopDetected = httpError{http.StatusLoopDetected}
LoopDetected represents the StatusLoopDetected HTTP error.
var MethodNotAllowed = httpError{http.StatusMethodNotAllowed}
MethodNotAllowed represents the StatusMethodNotAllowed HTTP error.
var MisdirectedRequest = httpError{http.StatusMisdirectedRequest}
MisdirectedRequest represents the StatusMisdirectedRequest HTTP error.
var NetworkAuthenticationRequired = httpError{http.StatusNetworkAuthenticationRequired}
NetworkAuthenticationRequired represents the StatusNetworkAuthenticationRequired HTTP error.
var NotAcceptable = httpError{http.StatusNotAcceptable}
NotAcceptable represents the StatusNotAcceptable HTTP error.
var NotExtended = httpError{http.StatusNotExtended}
NotExtended represents the StatusNotExtended HTTP error.
var NotFound = httpError{http.StatusNotFound}
NotFound represents the StatusNotFound HTTP error.
var NotImplemented = httpError{http.StatusNotImplemented}
NotImplemented represents the StatusNotImplemented HTTP error.
var Panic = panicError{}
var PaymentRequired = httpError{http.StatusPaymentRequired}
PaymentRequired represents the StatusPaymentRequired HTTP error.
var PreconditionFailed = httpError{http.StatusPreconditionFailed}
PreconditionFailed represents the StatusPreconditionFailed HTTP error.
var PreconditionRequired = httpError{http.StatusPreconditionRequired}
PreconditionRequired represents the StatusPreconditionRequired HTTP error.
var ProxyAuthRequired = httpError{http.StatusProxyAuthRequired}
ProxyAuthRequired represents the StatusProxyAuthRequired HTTP error.
var RequestEntityTooLarge = httpError{http.StatusRequestEntityTooLarge}
RequestEntityTooLarge represents the StatusRequestEntityTooLarge HTTP error.
var RequestHeaderFieldsTooLarge = httpError{http.StatusRequestHeaderFieldsTooLarge}
RequestHeaderFieldsTooLarge represents the StatusRequestHeaderFieldsTooLarge HTTP error.
var RequestTimeout = httpError{http.StatusRequestTimeout}
RequestTimeout represents the StatusRequestTimeout HTTP error.
var RequestURITooLong = httpError{http.StatusRequestURITooLong}
RequestURITooLong represents the StatusRequestURITooLong HTTP error.
var RequestedRangeNotSatisfiable = httpError{http.StatusRequestedRangeNotSatisfiable}
RequestedRangeNotSatisfiable represents the StatusRequestedRangeNotSatisfiable HTTP error.
ServiceUnavailable represents the StatusServiceUnavailable HTTP error.
var Teapot = httpError{http.StatusTeapot}
Teapot represents the StatusTeapot HTTP error.
var TooEarly = httpError{http.StatusTooEarly}
TooEarly represents the StatusTooEarly HTTP error.
var TooManyRequests = httpError{http.StatusTooManyRequests}
TooManyRequests represents the StatusTooManyRequests HTTP error.
Unauthorized represents the StatusUnauthorized HTTP error.
UnavailableForLegalReasons represents the StatusUnavailableForLegalReasons HTTP error.
var UnprocessableEntity = httpError{http.StatusUnprocessableEntity}
UnprocessableEntity represents the StatusUnprocessableEntity HTTP error.
var UnsupportedMediaType = httpError{http.StatusUnsupportedMediaType}
UnsupportedMediaType represents the StatusUnsupportedMediaType HTTP error.
var UpgradeRequired = httpError{http.StatusUpgradeRequired}
UpgradeRequired represents the StatusUpgradeRequired HTTP error.
var VariantAlsoNegotiates = httpError{http.StatusVariantAlsoNegotiates}
VariantAlsoNegotiates represents the StatusVariantAlsoNegotiates HTTP error.
Functions ¶
func DefaultErrorHandler ¶
func DefaultErrorHandler(w http.ResponseWriter, e error)
DefaultErrorHandler writes a reasonable default error response, using the status code from the error if it can be extracted (see StatusCode), or 500 by default, using the content type from from w.Header(), or text/html by default, and using any public message (see PublicErrorf and Public.)
func Errorf ¶
Errorf works like fmt.Errorf but it also embeds an HTTP status code. The status code can be extracted using httperror.StatusCode.
func New ¶
New constructs an error with an embedded an HTTP status code. The status code can be extracted using httperror.StatusCode.
func NewPublic ¶
NewPublic returns a new public error with the given status code and public error message generated using the format string and arguments. The resulting error value implements the the httperror.Public interface.
func PublicErrorf ¶
func PublicMessage ¶
PublicMessage extracts the public message from errors that have a `PUblicMessage() string` method.
func StatusCode ¶
StatusCode extracts the HTTP status code from an error created by this package. If the error doesn't have an embedded status code, it returns InternalServerError. If the error is nil, returns 200 OK.
func Wrap ¶
Wrap wraps an error and embeds an HTTP status code that can be extracted using httperror.StatusCode
func WrapHandlerFunc ¶
func WrapHandlerFunc(h func(w http.ResponseWriter, r *http.Request) error, eh ErrorHandler) http.HandlerFunc
WrapHandlerFunc wraps an HandlerFunc function with a custom error handler. Return a standard http.HandlerFunc since returning an error is irrelevant once it has been handled.
func WrapXHandlerFunc ¶
func WrapXHandlerFunc[P any](h func(w http.ResponseWriter, r *http.Request, p P) error, eh ErrorHandler) func(w http.ResponseWriter, r *http.Request, p P)
WrapXHandlerFunc constructs an httperror.XHandlerFunc with a custom error handler. Returns an function with the same signature but without the error return value.
func WriteResponse ¶
func WriteResponse(w http.ResponseWriter, s int, m []byte)
WriteResponse writes a reasonable default error response given the status code and optional error message. The default error handler DefaultErrorHandler calls this method after extracting the status code and any public error message.
Types ¶
type ErrorHandler ¶
type ErrorHandler = func(http.ResponseWriter, error)
ErrorHandler handles an error.
type Handler ¶
type Handler interface { ServeHTTP(w http.ResponseWriter, r *http.Request) Serve(w http.ResponseWriter, r *http.Request) error }
Handler is like the standard http.Handler interface type, but it also implements the Serve method which returns an error. When used as a standard http.Handler, any errors will be handled by the default error handler DefaultErrorHandler. But code that understands the httperror.Handler interface and can deal with returned errors can call the Serve method.
type HandlerFunc ¶
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
HandlerFunc is like the standard http.HandlerFunc type, but it returns an error. HandlerFunc implements both the httperror.Handler and the http.Handler interface.
func ApplyStandardMiddleware ¶ added in v1.2.0
func ApplyStandardMiddleware(h Handler, ms ...StandardMiddleware) HandlerFunc
ApplyStandardMiddleware applies middleware written for a standard http.Handler to an httperror.Handler, returning an httperror.Handler. It is possible to apply standard middleware to httperror.Handler without using this function,because httperror.Handler implements the standard http.Handler interface. However, the result would be an http.Handler, not an httperror.Handler, and so parameters could not passed to it and it could not return an error. This function solves that problem by passing errors and parameters through the context.
func PanicMiddleware ¶ added in v1.1.0
func PanicMiddleware(h Handler) HandlerFunc
PanicMiddleware wraps a httperror.Handler, returning a new httperror.HandlerFunc that recovers from panics and returns them as errors. Panic error can be identified using errors.Is(err, httperror.Panic)
func (HandlerFunc) Serve ¶
func (h HandlerFunc) Serve(w http.ResponseWriter, r *http.Request) error
Serve makes httperror.HandlerFunc implement the httperror.Handler interface
func (HandlerFunc) ServeHTTP ¶
func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP makes httperror.HandlerFunc implement the standard http.Handler interface. Any errors will be handled by the default error handler DefaultErrorHandler.
type Public ¶
type Public = interface {
PublicMessage() string
}
Public is an interface that requires a PublicMessage() string method. httperror.PublicMessage will extract the public error message from errors that implements this interface.
type StandardMiddleware ¶ added in v1.2.0
StandardMiddleware is a standard http.Handler wrapper.
type XHandler ¶
type XHandler[P any] interface { ServeHTTP(w http.ResponseWriter, r *http.Request) Serve(w http.ResponseWriter, r *http.Request, p P) error }
XHandler is a generic version of httperror.Handler. The Serve method which accepts a third generic parameter.
type XHandlerFunc ¶
XHandlerFunc is a generic version of httperror.HandlerFunc, that accepts a third generic parameter.
func XApplyStandardMiddleware ¶ added in v1.2.0
func XApplyStandardMiddleware[P any](h XHandler[P], ms ...StandardMiddleware) XHandlerFunc[P]
XApplyStandardMiddleware applies middleware written for a standard http.Handler to an httperror.XHandler, returning an httperror.XHandler. It is possible to apply standard middleware to httperror.XHandler without using this function,because httperror.XHandler implements the standard http.Handler interface. However, the result would be an http.Handler, not an httperror.XHandler, and so parameters could not passed to it and it could not return an error. This function solves that problem by passing errors and parameters through the context.
func XPanicMiddleware ¶ added in v1.1.0
func XPanicMiddleware[P any](h XHandler[P]) XHandlerFunc[P]
XPanicMiddleware wraps a httperror.XHandler, returning a new httperror.XHandlerFunc that recovers from panics and returns them as errors. Panic error can be identified using errors.Is(err, httperror.Panic)
func (XHandlerFunc[P]) Serve ¶
func (h XHandlerFunc[P]) Serve(w http.ResponseWriter, r *http.Request, p P) error
Serve makes httperror.XHandlerFunc implement the httperror.Handler interface
func (XHandlerFunc[P]) ServeHTTP ¶
func (h XHandlerFunc[P]) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP makes httperror.XHandlerFunc implement the standard http.Handler interface. Any errors will be handled by the default error handler DefaultErrorHandler.