httpserver

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 14 Imported by: 2

Documentation

Overview

Package httpserver holds the errors, error handlers, operation middleware and response writing of a generated server.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoResponse  = errors.New("the service returned no response")
	ErrResponseCut = errors.New("response cut short")
	ErrPanic       = errors.New("panic")
	ErrContextLost = errors.New("a middleware passed on a request without the context it got")
)

Functions

func AsError

func AsError[T error, P interface {
	*T
	error
}](err error) (T, bool)

AsError returns the T in err's chain, as a value or behind a pointer, when there is one.

func DetachPathValue

func DetachPathValue(v string) string

DetachPathValue unescapes v, cut from the raw path on such a server, into memory of its own.

func DetachRequest

func DetachRequest(r *http.Request) *http.Request

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

func ValidateResponse(v any) error

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 NewChain

func NewChain(mw ...func(http.Handler) http.Handler) Chain

NewChain wraps mw around the handler each request carries, outermost first.

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

type ErrorHandlerFunc func(w http.ResponseWriter, r *http.Request, status int, err error)

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
)

func (ErrorKind) String

func (k ErrorKind) String() string

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

type Writer

type Writer struct {
	Marshal func(v any) ([]byte, error)
}

Writer writes response bodies by their media type. Marshal writes JSON bodies and frames, json.Marshal when it is nil.

func (Writer) Write

func (wr Writer) Write(w http.ResponseWriter, status int, headers http.Header, body any) error

Write writes the headers, then body encoded for the media type of its Content-Type.

Jump to

Keyboard shortcuts

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