apperr

package
v0.0.0-...-1f1dc5f Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package apperr defines framework-free application errors.

Index

Constants

View Source
const (
	ErrSuccess int = iota + 100001
	ErrUnknown
	ErrBind
	ErrValidation
	ErrTokenInvalid
)

Common application error codes.

View Source
const (
	ErrDatabase int = iota + 100101
	ErrRedis
	ErrKafka
	ErrExternalService
)

Data source and external system error codes.

View Source
const (
	ErrBadRequest          = 100400
	ErrUnauthorized        = 100401
	ErrForbidden           = 100403
	ErrNotFound            = 100404
	ErrMethodNotAllowed    = 100405
	ErrConflict            = 100409
	ErrSystemUninitialized = 100410
	ErrInternalServer      = 100500
)

Common request error codes.

View Source
const (
	ErrEncrypt int = iota + 100201
	ErrSignatureInvalid
	ErrExpired
	ErrInvalidAuthHeader
	ErrMissingHeader
	ErrPasswordIncorrect
	ErrPermissionDenied
	ErrAccountLocked
	ErrAccountDisabled
	ErrTooManyAttempts
)

Authentication and authorization error codes.

View Source
const (
	ErrEncodingFailed int = iota + 100301
	ErrDecodingFailed
	ErrInvalidJSON
	ErrEncodingJSON
	ErrDecodingJSON
	ErrInvalidYaml
	ErrEncodingYaml
	ErrDecodingYaml
)

Encoding and decoding error codes.

Variables

This section is empty.

Functions

func HTTPStatus

func HTTPStatus(code int) int

HTTPStatus returns the HTTP status associated with a registered application error code. Unknown codes are treated as internal server errors.

func IsClientError

func IsClientError(code int) bool

IsClientError reports whether code maps to a 4xx HTTP status.

func IsInternalError

func IsInternalError(code int) bool

IsInternalError reports whether code should be handled as an internal error.

func IsServerError

func IsServerError(code int) bool

IsServerError reports whether code maps to a 5xx HTTP status.

func New

func New(code int, message string) error

New creates an application error.

func NewBadRequest

func NewBadRequest(message string) error

NewBadRequest creates a bad request error.

func NewConflict

func NewConflict(message string) error

NewConflict creates a conflict error.

func NewForbidden

func NewForbidden() error

NewForbidden creates a forbidden error.

func NewMethodNotAllowed

func NewMethodNotAllowed(message string) error

NewMethodNotAllowed creates a method-not-allowed error.

func NewNotFound

func NewNotFound(resource string) error

NewNotFound creates a not found error for resource.

func NewPermissionDenied

func NewPermissionDenied(resource, action string) error

NewPermissionDenied creates a permission-denied error.

func NewTokenExpired

func NewTokenExpired() error

NewTokenExpired creates an expired-token error.

func NewTokenInvalid

func NewTokenInvalid() error

NewTokenInvalid creates an invalid-token error.

func NewUnauthorized

func NewUnauthorized() error

NewUnauthorized creates an unauthorized error.

func NewValidation

func NewValidation(field, message string) error

NewValidation creates a validation error.

func Newf

func Newf(code int, format string, args ...interface{}) error

Newf creates a formatted application error.

func Wrap

func Wrap(err error, code int, message string) error

Wrap adds an application code and safe message to err.

func WrapBadRequest

func WrapBadRequest(err error, message string) error

WrapBadRequest wraps a bad request error.

func WrapConflict

func WrapConflict(err error, message string) error

WrapConflict wraps a conflict error.

func WrapDatabase

func WrapDatabase(err error, operation string) error

WrapDatabase wraps database errors.

func WrapExternal

func WrapExternal(err error, service, operation string) error

WrapExternal wraps external service errors.

func WrapForbidden

func WrapForbidden(err error, message string) error

WrapForbidden wraps a forbidden error.

func WrapInternal

func WrapInternal(err error, message string) error

WrapInternal wraps an internal server error.

func WrapKafka

func WrapKafka(err error, operation string) error

WrapKafka wraps Kafka errors.

func WrapMethodNotAllowed

func WrapMethodNotAllowed(err error, message string) error

WrapMethodNotAllowed wraps a method-not-allowed error.

func WrapNotFound

func WrapNotFound(err error, message string) error

WrapNotFound wraps a not found error.

func WrapRedis

func WrapRedis(err error, operation string) error

WrapRedis wraps Redis errors.

func WrapUnauthorized

func WrapUnauthorized(err error, message string) error

WrapUnauthorized wraps an unauthorized error.

func Wrapf

func Wrapf(err error, code int, format string, args ...interface{}) error

Wrapf adds an application code and formatted safe message to err.

Types

type Category

type Category string

Category identifies the operational area associated with an error.

const (
	CategorySystem     Category = "system"
	CategoryDatabase   Category = "database"
	CategoryRedis      Category = "redis"
	CategoryKafka      Category = "kafka"
	CategoryExternal   Category = "external"
	CategoryAuth       Category = "auth"
	CategoryPermission Category = "permission"
	CategoryValidation Category = "validation"
	CategoryBusiness   Category = "business"
)

Application error categories.

type Definition

type Definition struct {
	Code     int
	Kind     Kind
	Category Category
	Message  string
}

Definition is the registered meaning of an application error code.

func Lookup

func Lookup(code int) (Definition, bool)

Lookup returns the registered definition for code.

func ParseRegistered

func ParseRegistered(err error) (Definition, bool)

ParseRegistered returns the first registered application error definition in err's unwrap chain.

type Error

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

Error carries a code, safe message, diagnostic detail, and wrapped cause.

func Parse

func Parse(err error) (*Error, bool)

Parse returns the first application Error in err's unwrap chain.

func (*Error) Code

func (e *Error) Code() int

Code returns the application error code.

func (*Error) Detail

func (e *Error) Detail() string

Detail returns internal diagnostic detail for logs.

func (*Error) Error

func (e *Error) Error() string

Error returns the safe client-facing message.

func (*Error) Message

func (e *Error) Message() string

Message returns the safe client-facing message.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying cause.

type Info

type Info struct {
	Kind     Kind
	Category Category
	Code     int
	Message  string
	Detail   string
}

Info is the normalized application error information used by logs and delivery adapters.

func NewInfo

func NewInfo(err error) Info

NewInfo normalizes err into safe public information plus internal detail.

func (Info) IsBusiness

func (i Info) IsBusiness() bool

IsBusiness reports whether info represents a non-internal error.

func (Info) IsInternal

func (i Info) IsInternal() bool

IsInternal reports whether info represents an internal error.

type Kind

type Kind string

Kind describes the caller-visible class of an application error without depending on HTTP.

const (
	KindOK               Kind = "ok"
	KindBadRequest       Kind = "bad_request"
	KindValidation       Kind = "validation"
	KindUnauthorized     Kind = "unauthorized"
	KindForbidden        Kind = "forbidden"
	KindNotFound         Kind = "not_found"
	KindMethodNotAllowed Kind = "method_not_allowed"
	KindConflict         Kind = "conflict"
	KindInternal         Kind = "internal"
)

Application error kinds.

Jump to

Keyboard shortcuts

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