httperror

package module
v2.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2022 License: MIT Imports: 8 Imported by: 0

README

Please use the v1 branch of this package github.com/johnwarden/httperror.

This /v2 branch was created because of one small, ill conceived backwards-compatibility-breaking change. The latest version of this package uses the original v1 interface.

Documentation

Overview

Package httperror is for writing HTTP handlers that return errors instead of handling them directly.

Please use the v1 branch of this package at github.com/johnwarden/httperror. The v2 branch has been discontinued.

Index

Constants

This section is empty.

Variables

View Source
var BadGateway = httpError{http.StatusBadGateway}

BadGateway represents the StatusBadGateway HTTP error.

View Source
var BadRequest = httpError{http.StatusBadRequest}

BadRequest represents the StatusBadRequest HTTP error.

View Source
var Conflict = httpError{http.StatusConflict}

Conflict represents the StatusConflict HTTP error.

View Source
var ExpectationFailed = httpError{http.StatusExpectationFailed}

ExpectationFailed represents the StatusExpectationFailed HTTP error.

View Source
var FailedDependency = httpError{http.StatusFailedDependency}

FailedDependency represents the StatusFailedDependency HTTP error.

View Source
var Forbidden = httpError{http.StatusForbidden}

Forbidden represents the StatusForbidden HTTP error.

View Source
var GatewayTimeout = httpError{http.StatusGatewayTimeout}

GatewayTimeout represents the StatusGatewayTimeout HTTP error.

View Source
var Gone = httpError{http.StatusGone}

Gone represents the StatusGone HTTP error.

View Source
var HTTPVersionNotSupported = httpError{http.StatusHTTPVersionNotSupported}

HTTPVersionNotSupported represents the StatusHTTPVersionNotSupported HTTP error.

View Source
var InsufficientStorage = httpError{http.StatusInsufficientStorage}

InsufficientStorage represents the StatusInsufficientStorage HTTP error.

View Source
var InternalServerError = httpError{http.StatusInternalServerError}

InternalServerError represents the StatusInternalServerError HTTP error.

View Source
var LengthRequired = httpError{http.StatusLengthRequired}

LengthRequired represents the StatusLengthRequired HTTP error.

View Source
var Locked = httpError{http.StatusLocked}

Locked represents the StatusLocked HTTP error.

View Source
var LoopDetected = httpError{http.StatusLoopDetected}

LoopDetected represents the StatusLoopDetected HTTP error.

View Source
var MethodNotAllowed = httpError{http.StatusMethodNotAllowed}

MethodNotAllowed represents the StatusMethodNotAllowed HTTP error.

View Source
var MisdirectedRequest = httpError{http.StatusMisdirectedRequest}

MisdirectedRequest represents the StatusMisdirectedRequest HTTP error.

View Source
var NetworkAuthenticationRequired = httpError{http.StatusNetworkAuthenticationRequired}

NetworkAuthenticationRequired represents the StatusNetworkAuthenticationRequired HTTP error.

View Source
var NotAcceptable = httpError{http.StatusNotAcceptable}

NotAcceptable represents the StatusNotAcceptable HTTP error.

View Source
var NotExtended = httpError{http.StatusNotExtended}

NotExtended represents the StatusNotExtended HTTP error.

View Source
var NotFound = httpError{http.StatusNotFound}

NotFound represents the StatusNotFound HTTP error.

View Source
var NotImplemented = httpError{http.StatusNotImplemented}

NotImplemented represents the StatusNotImplemented HTTP error.

View Source
var PaymentRequired = httpError{http.StatusPaymentRequired}

PaymentRequired represents the StatusPaymentRequired HTTP error.

View Source
var PreconditionFailed = httpError{http.StatusPreconditionFailed}

PreconditionFailed represents the StatusPreconditionFailed HTTP error.

View Source
var PreconditionRequired = httpError{http.StatusPreconditionRequired}

PreconditionRequired represents the StatusPreconditionRequired HTTP error.

View Source
var ProxyAuthRequired = httpError{http.StatusProxyAuthRequired}

ProxyAuthRequired represents the StatusProxyAuthRequired HTTP error.

View Source
var RequestEntityTooLarge = httpError{http.StatusRequestEntityTooLarge}

RequestEntityTooLarge represents the StatusRequestEntityTooLarge HTTP error.

View Source
var RequestHeaderFieldsTooLarge = httpError{http.StatusRequestHeaderFieldsTooLarge}

RequestHeaderFieldsTooLarge represents the StatusRequestHeaderFieldsTooLarge HTTP error.

View Source
var RequestTimeout = httpError{http.StatusRequestTimeout}

RequestTimeout represents the StatusRequestTimeout HTTP error.

View Source
var RequestURITooLong = httpError{http.StatusRequestURITooLong}

RequestURITooLong represents the StatusRequestURITooLong HTTP error.

View Source
var RequestedRangeNotSatisfiable = httpError{http.StatusRequestedRangeNotSatisfiable}

RequestedRangeNotSatisfiable represents the StatusRequestedRangeNotSatisfiable HTTP error.

View Source
var ServiceUnavailable = httpError{http.StatusServiceUnavailable}

ServiceUnavailable represents the StatusServiceUnavailable HTTP error.

View Source
var Teapot = httpError{http.StatusTeapot}

Teapot represents the StatusTeapot HTTP error.

View Source
var TooEarly = httpError{http.StatusTooEarly}

TooEarly represents the StatusTooEarly HTTP error.

View Source
var TooManyRequests = httpError{http.StatusTooManyRequests}

TooManyRequests represents the StatusTooManyRequests HTTP error.

View Source
var Unauthorized = httpError{http.StatusUnauthorized}

Unauthorized represents the StatusUnauthorized HTTP error.

View Source
var UnavailableForLegalReasons = httpError{http.StatusUnavailableForLegalReasons}

UnavailableForLegalReasons represents the StatusUnavailableForLegalReasons HTTP error.

View Source
var UnprocessableEntity = httpError{http.StatusUnprocessableEntity}

UnprocessableEntity represents the StatusUnprocessableEntity HTTP error.

View Source
var UnsupportedMediaType = httpError{http.StatusUnsupportedMediaType}

UnsupportedMediaType represents the StatusUnsupportedMediaType HTTP error.

View Source
var UpgradeRequired = httpError{http.StatusUpgradeRequired}

UpgradeRequired represents the StatusUpgradeRequired HTTP error.

View Source
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

func Errorf(s int, format string, args ...interface{}) error

Errorf works like fmt.Errorf but it also embeds an HTTP status code. The status code can be extracted using httperror.StatusCode.

func New

func New(s int, m string) error

New constructs an error with an embedded an HTTP status code. The status code can be extracted using httperror.StatusCode.

func NewPublic

func NewPublic(status int, message string) error

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 PublicErrorf(status int, format string, args ...interface{}) error

func PublicMessage

func PublicMessage(err error) string

PublicMessage extracts the public message from errors that have a `PUblicMessage() string` method.

func StatusCode

func StatusCode(err error) int

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

func Wrap(err error, status int) error

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

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

func PanicMiddleware(h Handler, s func(error)) HandlerFunc

PanicMiddleware wraps a httperror.Handler, returning a new httperror.HandlerFunc that recovers from panics and returns them as errors. The second argument is an optional function that is called if there is a panic. This function can be used, for example, to cleanly shutdown the server.

func (HandlerFunc) Serve

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

type StandardMiddleware = func(http.Handler) http.Handler

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

type XHandlerFunc[P any] func(w http.ResponseWriter, r *http.Request, p P) error

XHandlerFunc is a generic version of httperror.HandlerFunc, that accepts a third generic parameter.

func XApplyStandardMiddleware

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

func XPanicMiddleware[P any](h XHandler[P], s func(error)) XHandlerFunc[P]

XPanicMiddleware wraps a httperror.XHandler, returning a new httperror.XHandlerFunc that recovers from panics and returns them as errors. The second argument is an optional function that is called if there is a panic. This function can be used, for example, to cleanly shutdown the server.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL