Documentation ¶
Overview ¶
Package httperr implements an error object that speaks HTTP.
Package httperr implements an error object that speaks HTTP.
Index ¶
- Variables
- func Client(next *http.Client, args ...ClientArg) *http.Client
- func DefaultClient() *http.Client
- func New(statusCode int, err error) error
- func Public(statusCode int, err error) error
- func ReportError(r *http.Request, err error)
- func StatusCodeAndText(err error) (int, string)
- func Write(w http.ResponseWriter, r *http.Request, err error)
- type ClientArg
- type HandlerFunc
- type Middleware
- type Response
- type Transport
- type Value
- type Writer
Constants ¶
This section is empty.
Variables ¶
var ( // BadRequest is an error that represents a static http.StatusBadRequest error BadRequest = Value{StatusCode: 400} Unauthorized = Value{StatusCode: 401} // PaymentRequired is an error that represents a static http.StatusPaymentRequired error PaymentRequired = Value{StatusCode: 402} // Forbidden is an error that represents a static http.StatusForbidden error Forbidden = Value{StatusCode: 403} // NotFound is an error that represents a static http.StatusNotFound error NotFound = Value{StatusCode: 404} // MethodNotAllowed is an error that represents a static http.StatusMethodNotAllowed error MethodNotAllowed = Value{StatusCode: 405} // NotAcceptable is an error that represents a static http.StatusNotAcceptable error NotAcceptable = Value{StatusCode: 406} // ProxyAuthRequired is an error that represents a static http.StatusProxyAuthRequired error ProxyAuthRequired = Value{StatusCode: 407} // RequestTimeout is an error that represents a static http.StatusRequestTimeout error RequestTimeout = Value{StatusCode: 408} // Conflict is an error that represents a static http.StatusConflict error Conflict = Value{StatusCode: 409} // Gone is an error that represents a static http.StatusGone error Gone = Value{StatusCode: 410} // LengthRequired is an error that represents a static http.StatusLengthRequired error LengthRequired = Value{StatusCode: 411} // PreconditionFailed is an error that represents a static http.StatusPreconditionFailed error PreconditionFailed = Value{StatusCode: 412} // RequestEntityTooLarge is an error that represents a static http.StatusRequestEntityTooLarge error RequestEntityTooLarge = Value{StatusCode: 413} // RequestURITooLong is an error that represents a static http.StatusRequestURITooLong error RequestURITooLong = Value{StatusCode: 414} // UnsupportedMediaType is an error that represents a static http.StatusUnsupportedMediaType error UnsupportedMediaType = Value{StatusCode: 415} // RequestedRangeNotSatisfiable is an error that represents a static http.StatusRequestedRangeNotSatisfiable error RequestedRangeNotSatisfiable = Value{StatusCode: 416} // ExpectationFailed is an error that represents a static http.StatusExpectationFailed error ExpectationFailed = Value{StatusCode: 417} // Teapot is an error that represents a static http.StatusTeapot error Teapot = Value{StatusCode: 418} // TooManyRequests is an error that represents a static http.StatusTooManyRequests error TooManyRequests = Value{StatusCode: 429} // InternalServerError is an error that represents a static http.StatusInternalServerError error InternalServerError = Value{StatusCode: 500} // NotImplemented is an error that represents a static http.StatusNotImplemented error NotImplemented = Value{StatusCode: 501} // BadGateway is an error that represents a static http.StatusBadGateway error BadGateway = Value{StatusCode: 502} ServiceUnavailable = Value{StatusCode: 503} // GatewayTimeout is an error that represents a static http.StatusGatewayTimeout error GatewayTimeout = Value{StatusCode: 504} // HTTPVersionNotSupported is an error that represents a static http.StatusHTTPVersionNotSupported error HTTPVersionNotSupported = Value{StatusCode: 505} )
Functions ¶
func DefaultClient ¶
DefaultClient returns an http.Client that wraps the default http.Client with an error handling transport.
func ReportError ¶
ReportError reports the error to the function given in OnError.
func StatusCodeAndText ¶
StatusCodeAndText returns the status code and text of the error
Types ¶
type HandlerFunc ¶
type HandlerFunc func(http.ResponseWriter, *http.Request) error
The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.
func (HandlerFunc) ServeHTTP ¶
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP calls f(w, r).
type Middleware ¶
type Middleware struct { // OnError is a function that is called then a request fails with an error. If this function // returns nil, then the error is assumed to be handled. If it returns a non-nil error, then // that error is written to the client with Write() OnError func(w http.ResponseWriter, r *http.Request, err error) error // Handler is the next handler Handler http.Handler }
Middleware wraps the provided handler with middleware that captures errors which are returned from HandlerFunc, or reported via ReportError, and invokes the provided callback to render them. If the handler returns a status code >= 400, the response is captured and passed to OnError as a Response.
func (Middleware) ServeHTTP ¶
func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Response ¶
Response is an alias for http.Response that implements the error interface. Example:
resp, err := http.Get("http://www.example.com") if err != nil { return err } if resp.StatusCode != http.StatusOK { return httperr.Response(*resp) } // ...
func (Response) WriteError ¶
func (re Response) WriteError(w http.ResponseWriter, r *http.Request)
WriteError copies the Response to the ResponseWriter.
type Transport ¶
type Transport struct { Next http.RoundTripper OnError func(req *http.Request, resp *http.Response) error }
Transport is an http.RoundTripper that intercepts responses where the StatusCode >= 400 and returns a Response{}.
If ErrorFactory is specified it should return an error that can be used to unmarshal a JSON error response. This is useful when a web service offers structured error information. If the error structure cannot be unmarshalled, then a regular Response error is returned.
type APIError struct { Code string `json:"code"` Message string `json:"message"` } func (a APIError) Error() string { return fmt.Sprintf("%s (%d)", a.Message, a.Code) } t := Transport{ ErrorFactory: func() error { return &APIError{} }, }
type Value ¶
type Value struct { Err error // the underlying error StatusCode int // the HTTP status code. If not supplied, http.StatusInternalServerError is used. Status string // the HTTP status text. If not supplied, http.StatusText(http.StatusCode) is used. Public bool Header http.Header // extra headers to add to the response (optional) }
Value is an Error that returns that status and code provided, and reveals the underlying wrapper error to the caller. The text of the error is rendered to the client in the body of the response, as well as in the X-Error header.
func (Value) StatusCodeAndText ¶
StatusCodeAndText returns the status code and text of the error
func (Value) WriteError ¶
func (e Value) WriteError(w http.ResponseWriter, r *http.Request)
WriteError writes an error response to w using the specified status code.