Documentation
¶
Overview ¶
Package httpserver holds the errors, error handlers, operation middleware and response writing of a generated server.
Index ¶
- Variables
- func AsError[T error, P interface{ ... }](err error) (T, bool)
- func DetachPathValue(v string) string
- func DetachRequest(r *http.Request) *http.Request
- func Recover(h http.Handler, eh ErrorHandler) http.Handler
- func ValidateResponse(v any) error
- type Chain
- type DefaultErrorHandler
- type ErrorHandler
- type ErrorHandlerFunc
- type ErrorKind
- type HandlerError
- type Writer
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func AsError ¶
AsError returns the T in err's chain, as a value or behind a pointer, when there is one.
func DetachPathValue ¶
DetachPathValue unescapes v, cut from the raw path on such a server, into memory of its own.
func DetachRequest ¶
DetachRequest copies r with strings of its own, for a server like fasthttp that reuses their memory.
func Recover ¶
func Recover(h http.Handler, eh ErrorHandler) http.Handler
Recover answers a panic in h with a 500 through eh, the error wrapping ErrPanic.
func ValidateResponse ¶
ValidateResponse checks v with its ValidateResponse method, else its Validate method, when it has one.
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain is middleware built once, which each request runs its own handler inside.
func (Chain) Serve ¶
func (c Chain) Serve(w http.ResponseWriter, r *http.Request, id string, serve http.HandlerFunc)
Serve puts id on the request's context as the operation name, then runs serve inside the middleware.
type DefaultErrorHandler ¶
type DefaultErrorHandler struct{}
DefaultErrorHandler writes an error as {"error": "..."}, or an error type of the spec as its own JSON, the Body of a HandlerError too, when the request accepts JSON, and as text otherwise.
func (DefaultErrorHandler) HandleError ¶
func (DefaultErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, status int, err error)
type ErrorHandler ¶
type ErrorHandler interface {
HandleError(w http.ResponseWriter, r *http.Request, status int, err error)
}
ErrorHandler writes the response of a failed request. err is a *HandlerError, or an error type of the spec returned by the service.
type ErrorHandlerFunc ¶
ErrorHandlerFunc is an ErrorHandler made of one function.
func (ErrorHandlerFunc) HandleError ¶
func (f ErrorHandlerFunc) HandleError(w http.ResponseWriter, r *http.Request, status int, err error)
type ErrorKind ¶
type ErrorKind int
ErrorKind says which step of handling a request failed.
const ( // ErrorParse is a parameter that could not be read. ErrorParse ErrorKind = iota // ErrorDecode is a body that could not be read. ErrorDecode // ErrorValidation is a request the spec rejects. ErrorValidation // ErrorService is an error the service returned. ErrorService // ErrorResponse is a response the spec rejects. ErrorResponse )
type HandlerError ¶
type HandlerError struct {
Kind ErrorKind
OperationID string
ParamName string
ParamLocation string
Status int
Err error
Body error
}
HandlerError is what a generated handler passes to the error handler when a request cannot be served. Status is the code to answer with, 0 for the one the kind implies. Err is the cause. Body is the error type of the spec that answers a request turned away, holding the message, when the operation documents one for the status.
func (*HandlerError) Error ¶
func (e *HandlerError) Error() string
Error describes the failure without the service's own error text, which may hold internals.
func (*HandlerError) StatusCode ¶
func (e *HandlerError) StatusCode() int
StatusCode is Status, or the code the kind implies: 400 for a bad request, 500 for the service and its response. A service out of time gets 503.
func (*HandlerError) Unwrap ¶
func (e *HandlerError) Unwrap() error