httperr

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 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,
	}
)

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 code VALIDATION_ERROR for dynamic validation messages.

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