errors

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadRequest      = New(http.StatusBadRequest, "bad_request", "bad request")
	ErrUnauthorized    = New(http.StatusUnauthorized, "unauthorized", "unauthorized")
	ErrForbidden       = New(http.StatusForbidden, "forbidden", "forbidden")
	ErrNotFound        = New(http.StatusNotFound, "not_found", "not found")
	ErrConflict        = New(http.StatusConflict, "conflict", "conflict")
	ErrValidation      = New(http.StatusUnprocessableEntity, "validation_failed", "validation failed")
	ErrTooManyRequests = New(http.StatusTooManyRequests, "too_many_requests", "too many requests")
	ErrInternal        = New(http.StatusInternalServerError, "internal_error", "internal server error")
)

Functions

func Render

func Render(w http.ResponseWriter, r *http.Request, err error, cfg RenderConfig)

Render writes an error response to the writer, negotiating content type based on the Accept header. In dev mode, stack traces are included.

Types

type Error

type Error struct {
	Status  int            `json:"status"`
	Code    string         `json:"code"`
	Message string         `json:"message"`
	Details map[string]any `json:"details,omitempty"`
	// contains filtered or unexported fields
}

Error represents a structured, typed error with an HTTP status code, machine-readable code, human-readable message, and optional details.

func New

func New(status int, code string, message string) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying cause, enabling errors.Is and errors.As.

func (*Error) WithCause

func (e *Error) WithCause(cause error) *Error

WithCause returns a new Error wrapping the given cause. The original error is not modified.

func (*Error) WithDetails

func (e *Error) WithDetails(details map[string]any) *Error

WithDetails returns a new Error with the given details map. The original error is not modified.

func (*Error) WithMessage

func (e *Error) WithMessage(message string) *Error

WithMessage returns a new Error with a different message. The original error is not modified.

type RenderConfig

type RenderConfig struct {
	IsDev bool
}

RenderConfig configures how errors are rendered in HTTP responses.

Jump to

Keyboard shortcuts

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