Documentation
¶
Overview ¶
Package errorx provides a structured, categorized error type with builder-style enrichment, sentinel matching, and HTTP status mapping.
Index ¶
- Variables
- func IsBadRequestError(err error) bool
- func IsBusinessError(err error) bool
- func IsForbiddenError(err error) bool
- func IsInternalError(err error) bool
- func IsNotFoundError(err error) bool
- func IsUnauthorizedError(err error) bool
- type Error
- func GetError(err error) *Error
- func NewBadRequestError(subject, message string, paramKeyVals ...any) *Error
- func NewBusinessError(subject, message string, paramKeyVals ...any) *Error
- func NewError(errorType ErrorType, subject, message string, paramKeyVals ...any) *Error
- func NewForbiddenError(subject, message string, paramKeyVals ...any) *Error
- func NewInternalError(subject, message string, paramKeyVals ...any) *Error
- func NewNotFoundError(subject, message string, paramKeyVals ...any) *Error
- func NewUnauthorizedError(subject, message string, paramKeyVals ...any) *Error
- func (e *Error) Error() string
- func (e *Error) Is(target error) bool
- func (e *Error) MarshalJSON() ([]byte, error)
- func (e *Error) Unwrap() error
- func (e *Error) WithCode(code int) *Error
- func (e *Error) WithInner(inner error) *Error
- func (e *Error) WithMessage(message string, params ...any) *Error
- func (e *Error) WithParams(keyvals ...any) *Error
- func (e *Error) WithSubject(subject string) *Error
- type ErrorResponse
- type ErrorType
Constants ¶
This section is empty.
Variables ¶
var ( ErrBadRequest = NewBadRequestError("General", "Bad Request") ErrNotFound = NewNotFoundError("General", "Not Found") ErrBusiness = NewBusinessError("General", "Business") ErrForbidden = NewForbiddenError("General", "Forbidden") ErrInternal = NewInternalError("General", "Internal") )
Functions ¶
func IsBadRequestError ¶
func IsBusinessError ¶
func IsForbiddenError ¶
func IsInternalError ¶
func IsNotFoundError ¶
func IsUnauthorizedError ¶
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 NewBadRequestError ¶
func NewBusinessError ¶
func NewError ¶
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 NewInternalError ¶
func NewNotFoundError ¶
func NewUnauthorizedError ¶
func (*Error) Is ¶
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 (*Error) WithMessage ¶
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 ¶
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 ¶
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.