httperr

package
v0.1.3 Latest Latest
Warning

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

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

Documentation

Overview

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

HTTPError carries status code and application error code (e.g. BAD_REQUEST, NOT_FOUND). CodeFromStatus maps HTTP status codes to default application codes. Use New for custom errors and NewValidationErrorf for validation failures. Sentinel errors (ErrInvalidID, ErrNotAuthenticated) are provided for common cases.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidID is a 400 error with code INVALID_ID (e.g. invalid UUID in path).
	ErrInvalidID = &HTTPError{
		Err:        errors.New("invalid ID"),
		StatusCode: http.StatusBadRequest,
		Code:       "INVALID_ID",
		IsExpected: true,
	}
	// ErrNotAuthenticated is a 401 error with code NOT_AUTHENTICATED.
	ErrNotAuthenticated = &HTTPError{
		Err:        errors.New("not authenticated"),
		StatusCode: http.StatusUnauthorized,
		Code:       "NOT_AUTHENTICATED",
		IsExpected: true,
	}
	// ErrForbidden is a 403 error with code FORBIDDEN.
	ErrForbidden = &HTTPError{
		Err:        errors.New("forbidden"),
		StatusCode: http.StatusForbidden,
		Code:       "FORBIDDEN",
		IsExpected: true,
	}
	// ErrNotFound is a 404 error with code NOT_FOUND.
	ErrNotFound = &HTTPError{
		Err:        errors.New("not found"),
		StatusCode: http.StatusNotFound,
		Code:       "NOT_FOUND",
		IsExpected: true,
	}
	// ErrConflict is a 409 error with code CONFLICT.
	ErrConflict = &HTTPError{
		Err:        errors.New("conflict"),
		StatusCode: http.StatusConflict,
		Code:       "CONFLICT",
		IsExpected: true,
	}
	// ErrGone is a 410 error with code GONE.
	ErrGone = &HTTPError{
		Err:        errors.New("gone"),
		StatusCode: http.StatusGone,
		Code:       "GONE",
		IsExpected: true,
	}
	// ErrUnprocessableEntity is a 422 error with code VALIDATION_ERROR.
	ErrUnprocessableEntity = &HTTPError{
		Err:        errors.New("unprocessable entity"),
		StatusCode: http.StatusUnprocessableEntity,
		Code:       "VALIDATION_ERROR",
		IsExpected: true,
	}
	// ErrTooManyRequests is a 429 error with code RATE_LIMIT_EXCEEDED.
	ErrTooManyRequests = &HTTPError{
		Err:        errors.New("too many requests"),
		StatusCode: http.StatusTooManyRequests,
		Code:       "RATE_LIMIT_EXCEEDED",
		IsExpected: true,
	}
	// ErrServiceUnavailable is a 503 error with code SERVICE_UNAVAILABLE.
	ErrServiceUnavailable = &HTTPError{
		Err:        errors.New("service unavailable"),
		StatusCode: http.StatusServiceUnavailable,
		Code:       "SERVICE_UNAVAILABLE",
		IsExpected: false,
	}
)

Functions

func CodeFromStatus added in v0.1.1

func CodeFromStatus(status int) string

CodeFromStatus returns the application error code for a given HTTP status.

func IsExpectedClientError added in v0.1.2

func IsExpectedClientError(err error) bool

Types

type HTTPError

type HTTPError struct {
	Err        error
	StatusCode int
	Code       string
	// IsExpected is true for client errors (4xx); callers may use it to avoid logging as server errors.
	IsExpected bool
}

HTTPError represents an error with HTTP status and application code.

func New

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

New returns an HTTPError with the given error, status code, and code. IsExpected is true for 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

func (*HTTPError) GetCode added in v0.1.1

func (e *HTTPError) GetCode() string

func (*HTTPError) HTTPStatus

func (e *HTTPError) HTTPStatus() int

func (*HTTPError) Unwrap

func (e *HTTPError) Unwrap() error

Jump to

Keyboard shortcuts

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