Help us shape the future of Go! Take the 2026 Go Developer Survey

errs

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 6 Imported by: 3

Documentation

Overview

Package errs defines the error values handlers return. A central handler (see package web) turns them into HTML pages, redirects with flashed errors, HTMX fragments or RFC 9457 problem details.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadRequest           = &HTTPError{Status: http.StatusBadRequest}
	ErrUnauthorized         = &HTTPError{Status: http.StatusUnauthorized}
	ErrForbidden            = &HTTPError{Status: http.StatusForbidden}
	ErrNotFound             = &HTTPError{Status: http.StatusNotFound}
	ErrMethodNotAllowed     = &HTTPError{Status: http.StatusMethodNotAllowed}
	ErrConflict             = &HTTPError{Status: http.StatusConflict}
	ErrPreconditionFailed   = &HTTPError{Status: http.StatusPreconditionFailed}
	ErrRequestTooLarge      = &HTTPError{Status: http.StatusRequestEntityTooLarge}
	ErrUnsupportedMediaType = &HTTPError{Status: http.StatusUnsupportedMediaType}
	ErrUnprocessable        = &HTTPError{Status: http.StatusUnprocessableEntity}
	ErrTooManyRequests      = &HTTPError{Status: http.StatusTooManyRequests}
	ErrInternal             = &HTTPError{Status: http.StatusInternalServerError}
)

Sentinel errors for common statuses. Compare with errors.Is.

Functions

func Status

func Status(err error) int

Status returns the HTTP status for err: the status of an *HTTPError, 422 for a *ValidationError, the status of a StatusCoder, and 500 otherwise.

Types

type HTTPError

type HTTPError struct {
	// Status is the HTTP status code.
	Status int
	// Message is safe to show to clients. Empty means the status text.
	Message string
	// Err is the internal cause; it is logged, never shown to clients.
	Err error
	// Headers are added to the response (e.g. Allow, Retry-After).
	Headers http.Header
}

HTTPError is an error with an HTTP status and a client-safe message.

func New

func New(status int, message string) *HTTPError

New returns an *HTTPError with status and a client-safe message.

func Wrap

func Wrap(status int, err error) *HTTPError

Wrap returns an *HTTPError with status whose internal cause is err.

func (*HTTPError) Error

func (e *HTTPError) Error() string

Error implements error.

func (*HTTPError) Is

func (e *HTTPError) Is(target error) bool

Is matches another *HTTPError with the same status, so sentinel errors work with errors.Is even when wrapped or re-created with a cause.

func (*HTTPError) Text

func (e *HTTPError) Text() string

Text returns Message or, when empty, the standard status text.

func (*HTTPError) Unwrap

func (e *HTTPError) Unwrap() error

Unwrap returns the internal cause.

func (*HTTPError) WithHeader

func (e *HTTPError) WithHeader(key, value string) *HTTPError

WithHeader returns a copy of e with an extra response header.

type StatusCoder

type StatusCoder interface {
	error
	HTTPStatus() int
}

StatusCoder is implemented by errors that carry their own HTTP status, such as errors from packages that should not import errs.

type ValidationError

type ValidationError struct {
	Fields map[string][]string
}

ValidationError reports invalid input, keyed by field path.

func NewValidation

func NewValidation() *ValidationError

NewValidation returns an empty ValidationError.

func (*ValidationError) Add

func (e *ValidationError) Add(field, message string) *ValidationError

Add appends a message for field.

func (*ValidationError) Empty

func (e *ValidationError) Empty() bool

Empty reports whether no field has errors.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error implements error.

func (*ValidationError) First

func (e *ValidationError) First(field string) string

First returns the first message for field, or "".

Jump to

Keyboard shortcuts

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