web

package module
v0.3.12 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 5 Imported by: 1

README

Helpers for implementing http.Handlers

Response Handlers

Using respond.WithRequest() we compute our options and PreferredContentType() tells one how to encode the data.

Content Negotiation

QualityList

The QualityList parser allows choosing the best option during Content Negotiation, e.g. accepted Content-Types.

BestQuality

qlist offers two helpers to choose the best option from a QualityList and a list of supported options, BestQuality() and BestQualityWithIdentity(). Identity is an special option we consider unless it's explicitly forbidden.

BestEncoding

qlist.BestEncoding() is a special case of BestQualityWithIdentity() using the Accept header, and falling back to "identity" as magic type.

See also

Documentation

Overview

Package web contains helpers for web handlers

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorText

func ErrorText(code int) string

ErrorText returns the title corresponding to a given HTTP Status code

func WithErrorHandler

func WithErrorHandler(ctx context.Context, h ErrorHandlerFunc) context.Context

WithErrorHandler attaches an ErrorHandler function to a context for later retrieval

Types

type Error

type Error interface {
	Error() string
	Status() int
}

Error is an error that knows its HTTP Status Code

type ErrorHandlerFunc

type ErrorHandlerFunc func(http.ResponseWriter, *http.Request, error)

ErrorHandlerFunc is the signature of a function used as ErrorHandler

func ErrorHandler

func ErrorHandler(ctx context.Context) (ErrorHandlerFunc, bool)

ErrorHandler attempts to pull an ErrorHandler from the context.Context

type HTTPError

type HTTPError struct {
	Err  error
	Code int
	Hdr  http.Header
}

HTTPError extends core.WrappedError with HTTP Status Code

func NewHTTPError

func NewHTTPError(code int, err error, note string) *HTTPError

NewHTTPError creates a new HTTPError with a given StatusCode and optional cause and annotation

func NewHTTPErrorf

func NewHTTPErrorf(code int, err error, format string, args ...any) *HTTPError

NewHTTPErrorf creates a new HTTPError with a given StatusCode and optional cause and formatted annotation

func (*HTTPError) AddHeader added in v0.3.6

func (err *HTTPError) AddHeader(key, value string)

AddHeader appends a value to an HTTP header entry of the HTTPError

func (*HTTPError) DeleteHeader added in v0.3.6

func (err *HTTPError) DeleteHeader(key string)

DeleteHeader removes a header key from the HTTPError if present

func (*HTTPError) Error

func (err *HTTPError) Error() string

func (*HTTPError) Header added in v0.3.6

func (err *HTTPError) Header() http.Header

Header returns a http.Header attached to this error for custom fields

func (*HTTPError) ServeHTTP

func (err *HTTPError) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP is a very primitive handler that will try to pass the error to a [middleware.ErrorHandlerFunc] provided via the request's context.Context

func (*HTTPError) SetHeader added in v0.3.6

func (err *HTTPError) SetHeader(key, value string)

SetHeader sets the value of a header key of the HTTPError

func (*HTTPError) Status

func (err *HTTPError) Status() int

Status returns the StatusCode associated with the Error

func (*HTTPError) Unwrap

func (err *HTTPError) Unwrap() error

Directories

Path Synopsis
Package qlist provides a processor for HTTP Quality Lists
Package qlist provides a processor for HTTP Quality Lists
Package respond assists handlers to produce reponses
Package respond assists handlers to produce reponses

Jump to

Keyboard shortcuts

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