errorx

package
v0.33.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package errorx provides a structured, categorized error type with builder-style enrichment, sentinel matching, and HTTP status mapping.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadRequest   = NewBadRequestError("General", "Bad Request")
	ErrNotFound     = NewNotFoundError("General", "Not Found")
	ErrBusiness     = NewBusinessError("General", "Business")
	ErrUnauthorized = NewUnauthorizedError("General", "Unauthorized")
	ErrForbidden    = NewForbiddenError("General", "Forbidden")
	ErrInternal     = NewInternalError("General", "Internal")
)

Functions

func IsBadRequestError

func IsBadRequestError(err error) bool

func IsBusinessError

func IsBusinessError(err error) bool

func IsForbiddenError

func IsForbiddenError(err error) bool

func IsInternalError

func IsInternalError(err error) bool

func IsNotFoundError

func IsNotFoundError(err error) bool

func IsUnauthorizedError

func IsUnauthorizedError(err error) bool

Types

type Error

type Error struct {
	Type    ErrorType      `json:"type"`
	Subject string         `json:"subject"`
	Message string         `json:"message"`
	Params  map[string]any `json:"params,omitempty"`
	Code    int            `json:"code"`
	Inner   error          `json:"-"`
}

func GetError

func GetError(err error) *Error

func NewBadRequestError

func NewBadRequestError(subject, message string, paramKeyVals ...any) *Error

func NewBusinessError

func NewBusinessError(subject, message string, paramKeyVals ...any) *Error

func NewError

func NewError(errorType ErrorType, subject, message string, paramKeyVals ...any) *Error

NewError builds an *Error. Optional key-value pairs are merged into Params (same semantics as WithParams): keys must be strings; a non-string key is surfaced under "!BADKEY", and a trailing key without a value is dropped.

func NewForbiddenError

func NewForbiddenError(subject, message string, paramKeyVals ...any) *Error

func NewInternalError

func NewInternalError(subject, message string, paramKeyVals ...any) *Error

func NewNotFoundError

func NewNotFoundError(subject, message string, paramKeyVals ...any) *Error

func NewUnauthorizedError

func NewUnauthorizedError(subject, message string, paramKeyVals ...any) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

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

Is reports whether e matches target for errors.Is, letting a typed *Error be used as a sentinel by category. target matches when it is an *Error of the same Type; if target also sets a non-zero Code it must match, and if target sets a Subject other than the default "General" it must match too. This makes the package sentinels match any error of their category — errors.Is(err, ErrNotFound) is true for any NotFound error — while a custom sentinel carrying a Subject and/or Code matches more narrowly. Wrapped non-Error sentinels still match through Unwrap as usual.

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

func (*Error) Unwrap

func (e *Error) Unwrap() error

func (*Error) WithCode

func (e *Error) WithCode(code int) *Error

func (*Error) WithInner

func (e *Error) WithInner(inner error) *Error

func (*Error) WithMessage

func (e *Error) WithMessage(message string, params ...any) *Error

WithMessage returns a copy of the error with the message replaced. Optional key-value pairs are merged into Params (same semantics as WithParams).

func (*Error) WithParams

func (e *Error) WithParams(keyvals ...any) *Error

WithParams returns a copy of the error with additional key-value pairs merged into Params. Keys must be strings; a non-string key is surfaced under "!BADKEY".

func (*Error) WithSubject

func (e *Error) WithSubject(subject string) *Error

type ErrorResponse

type ErrorResponse struct {
	Error      string         `json:"error"`
	Subject    string         `json:"subject"`
	Message    string         `json:"message"`
	Params     map[string]any `json:"params,omitempty"`
	Code       int            `json:"code"`
	InnerError *string        `json:"innerError,omitempty"`
}

ErrorResponse is the JSON body returned by the HTTP error middleware.

type ErrorType

type ErrorType string

ErrorType identifies the category of an error and controls HTTP status mapping: BadRequest→400, Unauthorized→401, Forbidden→403, NotFound→404, Business→409, Internal→500.

const (
	BadRequestErrorType   ErrorType = "BAD_REQUEST"
	NotFoundErrorType     ErrorType = "NOT_FOUND"
	BusinessErrorType     ErrorType = "BUSINESS"
	UnauthorizedErrorType ErrorType = "UNAUTHORIZED"
	ForbiddenErrorType    ErrorType = "FORBIDDEN"
	InternalErrorType     ErrorType = "INTERNAL"
)

func GetErrorType

func GetErrorType(err error) (ErrorType, bool)

Jump to

Keyboard shortcuts

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