httperr

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package httperr provides HTTP-aware error types for JSON APIs.

HTTPError

HTTPError carries an HTTP status code and an application error code (e.g. BAD_REQUEST, NOT_FOUND). Use New to build a custom error from an underlying error, status, and code. IsExpected is true for 4xx so callers can avoid logging them as server errors. Implementations of error, HTTPStatus, and GetCode allow handlers to respond with the correct status and JSON body.

Status-to-code mapping

CodeFromStatus returns a default application code for a given HTTP status (e.g. 404 -> "NOT_FOUND"). Use it when rendering errors that do not implement the status/code interface.

Validation errors

NewValidationErrorf builds an HTTPError with status 400 and code VALIDATION_ERROR for dynamic messages. For "valid JSON but business validation failed" use ErrUnprocessableEntity (422) instead.

Sentinel errors

ErrInvalidID, ErrNotAuthenticated, ErrForbidden, ErrNotFound, ErrConflict, ErrGone, ErrUnprocessableEntity, ErrTooManyRequests, and ErrServiceUnavailable return ready-made *HTTPError for common cases. IsExpectedClientError reports whether an error is a 4xx client error.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidIDSentinel           = errors.New("invalid ID")
	ErrNotAuthenticatedSentinel    = errors.New("not authenticated")
	ErrForbiddenSentinel           = errors.New("forbidden")
	ErrNotFoundSentinel            = errors.New("not found")
	ErrConflictSentinel            = errors.New("conflict")
	ErrGoneSentinel                = errors.New("gone")
	ErrUnprocessableEntitySentinel = errors.New("unprocessable entity")
	ErrTooManyRequestsSentinel     = errors.New("too many requests")
	ErrServiceUnavailableSentinel  = errors.New("service unavailable")
)

Sentinel error values used as HTTPError.Unwrap() targets by the Err* functions below.

Functions

func CodeFromStatus added in v0.1.1

func CodeFromStatus(status int) string

CodeFromStatus returns the default application error code for the given HTTP status (e.g. 404 -> "NOT_FOUND", 500 -> "INTERNAL_ERROR").

func IsExpectedClientError added in v0.1.2

func IsExpectedClientError(err error) bool

IsExpectedClientError reports whether err is an HTTPError with status 4xx (client error).

Types

type HTTPError

type HTTPError struct {
	// contains filtered or unexported fields
}

HTTPError is an error that carries HTTP status and application error code for JSON responses.

func ErrConflict added in v0.1.3

func ErrConflict() *HTTPError

ErrConflict returns an HTTPError with status 409 and code CONFLICT.

func ErrForbidden added in v0.1.3

func ErrForbidden() *HTTPError

ErrForbidden returns an HTTPError with status 403 and code FORBIDDEN.

func ErrGone added in v0.1.3

func ErrGone() *HTTPError

ErrGone returns an HTTPError with status 410 and code GONE.

func ErrInvalidID

func ErrInvalidID() *HTTPError

ErrInvalidID returns an HTTPError with status 400 and code INVALID_ID.

func ErrNotAuthenticated

func ErrNotAuthenticated() *HTTPError

ErrNotAuthenticated returns an HTTPError with status 401 and code NOT_AUTHENTICATED.

func ErrNotFound added in v0.1.3

func ErrNotFound() *HTTPError

ErrNotFound returns an HTTPError with status 404 and code NOT_FOUND.

func ErrServiceUnavailable added in v0.1.3

func ErrServiceUnavailable() *HTTPError

ErrServiceUnavailable returns an HTTPError with status 503 and code SERVICE_UNAVAILABLE.

func ErrTooManyRequests added in v0.1.3

func ErrTooManyRequests() *HTTPError

ErrTooManyRequests returns an HTTPError with status 429 and code RATE_LIMIT_EXCEEDED.

func ErrUnprocessableEntity added in v0.1.3

func ErrUnprocessableEntity() *HTTPError

ErrUnprocessableEntity returns an HTTPError with status 422 and code VALIDATION_ERROR.

func New

func New(err error, status int, code string) *HTTPError

New builds an HTTPError with the given underlying error, HTTP status, and application code. IsClientError is set to true when status is 4xx.

func NewValidationErrorf

func NewValidationErrorf(format string, args ...any) *HTTPError

NewValidationErrorf creates an HTTPError with status 400 and code VALIDATION_ERROR for dynamic validation messages. For semantic "request body valid JSON but business validation failed" use ErrUnprocessableEntity (422) from sentinels.

func (*HTTPError) Error

func (e *HTTPError) Error() string

Error implements error and returns the wrapped error's message.

func (*HTTPError) GetCode added in v0.1.1

func (e *HTTPError) GetCode() string

GetCode returns the application error code for the JSON body.

func (*HTTPError) HTTPStatus

func (e *HTTPError) HTTPStatus() int

HTTPStatus returns the HTTP status code to send in the response.

func (*HTTPError) IsClientError added in v0.2.0

func (e *HTTPError) IsClientError() bool

IsClientError reports whether this error represents a client error (4xx).

func (*HTTPError) Unwrap

func (e *HTTPError) Unwrap() error

Unwrap returns the wrapped error for errors.Is/As.

Jump to

Keyboard shortcuts

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