{"modulePath":"github.com/mockzilla/mockzilla-codegen","version":"v1.0.2","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package httpserver\n\nPackage httpserver holds the errors, error handlers, operation middleware and response writing of a generated server.\n\n## Variables\n\n```go\nvar (\n\tErrNoResponse  = errors.New(\"the service returned no response\")\n\tErrResponseCut = errors.New(\"response cut short\")\n\tErrPanic       = errors.New(\"panic\")\n\tErrContextLost = errors.New(\"a middleware passed on a request without the context it got\")\n)\n```\n\n## Functions\n\n```go\nfunc AsError[T error, P interface {\n\t*T\n\terror\n}](err error) (T, bool)\n```\nAsError returns the T in err's chain, as a value or behind a pointer, when there is one.\n\n```go\nfunc DetachPathValue(v string) string\n```\nDetachPathValue unescapes v, cut from the raw path on such a server, into memory of its own.\n\n```go\nfunc DetachRequest(r *http.Request) *http.Request\n```\nDetachRequest copies r with strings of its own, for a server like fasthttp that reuses their memory.\n\n```go\nfunc Recover(h http.Handler, eh ErrorHandler) http.Handler\n```\nRecover answers a panic in h with a 500 through eh, the error wrapping ErrPanic.\n\n```go\nfunc ValidateResponse(v any) error\n```\nValidateResponse checks v with its ValidateResponse method, else its Validate method, when it has one.\n\n## Types\n\n```go\ntype Chain struct {\n\t// contains filtered or unexported fields\n}\n```\nChain is middleware built once, which each request runs its own handler inside.\n\n```go\nfunc NewChain(mw ...func(http.Handler) http.Handler) Chain\n```\nNewChain wraps mw around the handler each request carries, outermost first.\n\n```go\nfunc (c Chain) Serve(w http.ResponseWriter, r *http.Request, id string, serve http.HandlerFunc)\n```\nServe puts id on the request's context as the operation name, then runs serve inside the middleware.\n\n```go\ntype DefaultErrorHandler struct{}\n```\nDefaultErrorHandler 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.\n\n```go\nfunc (DefaultErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, status int, err error)\n```\n\n```go\ntype ErrorHandler interface {\n\tHandleError(w http.ResponseWriter, r *http.Request, status int, err error)\n}\n```\nErrorHandler writes the response of a failed request. err is a \\*HandlerError, or an error type of the spec returned by the service.\n\n```go\ntype ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, status int, err error)\n```\nErrorHandlerFunc is an ErrorHandler made of one function.\n\n```go\nfunc (f ErrorHandlerFunc) HandleError(w http.ResponseWriter, r *http.Request, status int, err error)\n```\n\n```go\ntype ErrorKind int\n```\nErrorKind says which step of handling a request failed.\n\n```go\nconst (\n\t// ErrorParse is a parameter that could not be read.\n\tErrorParse ErrorKind = iota\n\t// ErrorDecode is a body that could not be read.\n\tErrorDecode\n\t// ErrorValidation is a request the spec rejects.\n\tErrorValidation\n\t// ErrorService is an error the service returned.\n\tErrorService\n\t// ErrorResponse is a response the spec rejects.\n\tErrorResponse\n)\n```\n\n```go\nfunc (k ErrorKind) String() string\n```\n\n```go\ntype HandlerError struct {\n\tKind          ErrorKind\n\tOperationID   string\n\tParamName     string\n\tParamLocation string\n\tStatus        int\n\tErr           error\n\tBody          error\n}\n```\nHandlerError 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.\n\n```go\nfunc (e *HandlerError) Error() string\n```\nError describes the failure without the service's own error text, which may hold internals.\n\n```go\nfunc (e *HandlerError) StatusCode() int\n```\nStatusCode 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.\n\n```go\nfunc (e *HandlerError) Unwrap() error\n```\n\n```go\ntype Writer struct {\n\tMarshal func(v any) ([]byte, error)\n}\n```\nWriter writes response bodies by their media type. Marshal writes JSON bodies and frames, json.Marshal when it is nil.\n\n```go\nfunc (wr Writer) Write(w http.ResponseWriter, status int, headers http.Header, body any) error\n```\nWrite writes the headers, then body encoded for the media type of its Content-Type.\n\n","path":"github.com/mockzilla/mockzilla-codegen/pkg/runtime/httpserver","name":"httpserver","synopsis":"Package httpserver holds the errors, error handlers, operation middleware and response writing of a generated server.","isRedistributable":true}
