Documentation
¶
Index ¶
- Variables
- func GetMeta(err error) map[string]any
- func HasCode(err error, code Code) bool
- func IsAuthentication(err error) bool
- func IsAuthorization(err error) bool
- func IsConflict(err error) bool
- func IsInternal(err error) bool
- func IsKind(err error, kind Kind) bool
- func IsNotFound(err error) bool
- func IsRateLimited(err error) bool
- func IsValidation(err error) bool
- type AppError
- type Code
- type Definition
- func (d *Definition) Code() Code
- func (d *Definition) Error() string
- func (d *Definition) Is(target error) bool
- func (d *Definition) Kind() Kind
- func (d *Definition) Message() string
- func (d *Definition) WithMessage(message string) *Error
- func (d *Definition) WithMeta(key string, value any) *Error
- func (d *Definition) Wrap(cause error) *Error
- type Error
- func (e *Error) Code() Code
- func (e *Error) Error() string
- func (e *Error) Is(target error) bool
- func (e *Error) Kind() Kind
- func (e *Error) Message() string
- func (e *Error) Meta() map[string]any
- func (e *Error) Unwrap() error
- func (e *Error) WithCause(cause error) *Error
- func (e *Error) WithMeta(key string, value any) *Error
- type Kind
Constants ¶
This section is empty.
Variables ¶
var ( // Validation ErrValidation = Define(KindValidation, "VALIDATION_ERROR", "validation error") ErrInvalidInput = Define(KindValidation, "INVALID_INPUT", "invalid input") ErrRequiredField = Define(KindValidation, "REQUIRED_FIELD", "required field is missing") ErrInvalidFormat = Define(KindValidation, "INVALID_FORMAT", "invalid format") // Not Found ErrNotFound = Define(KindNotFound, "NOT_FOUND", "resource not found") ErrRecordNotFound = Define(KindNotFound, "RECORD_NOT_FOUND", "record not found") // Conflict ErrConflict = Define(KindConflict, "CONFLICT", "resource already exists") ErrDuplicateEntry = Define(KindConflict, "DUPLICATE_ENTRY", "duplicate entry") // Authentication ErrUnauthenticated = Define(KindAuthentication, "UNAUTHENTICATED", "authentication required") ErrTokenInvalid = Define(KindAuthentication, "TOKEN_INVALID", "token is invalid") ErrTokenExpired = Define(KindAuthentication, "TOKEN_EXPIRED", "token has expired") ErrTokenRequired = Define(KindAuthentication, "TOKEN_REQUIRED", "token is required") // Authorization ErrForbidden = Define(KindAuthorization, "FORBIDDEN", "access denied") // Internal ErrInternal = Define(KindInternal, "INTERNAL_ERROR", "internal server error") // Rate Limit ErrRateLimited = Define(KindRateLimited, "RATE_LIMITED", "rate limit exceeded") )
Generic sentinel errors — common errors reusable in any project. For business-specific errors, define in your project with Define().
Functions ¶
func GetMeta ¶
GetMeta extracts the meta from an error chain. Returns nil if no *Error is found. Only *Error carries meta — *Definition and plain errors return nil.
func IsAuthentication ¶
func IsAuthorization ¶
func IsConflict ¶
func IsInternal ¶
func IsKind ¶
IsKind checks if an error (or any error in its chain) is an AppError with the specified Kind. Uses errors.As under the hood, so it works with wrapped errors (e.g., fmt.Errorf("...: %w", err)).
func IsNotFound ¶
func IsRateLimited ¶
func IsValidation ¶
Types ¶
type AppError ¶
AppError is the common interface between Definition (static error) and Error (error with context). Both can be returned as error. Helpers like IsNotFound(), AsAppError(), and the httpmap package check for this interface.
func AsAppError ¶
AsAppError extracts the AppError from an error chain. Works with both *Definition and *Error, and traverses wrapped errors.
type Code ¶
type Code string
Code is a unique and stable identifier for each system error. The frontend uses this code to translate messages (i18n). Convention: "DOMAIN_ACTION_REASON" (e.g., AUTH_LOGIN_BLOCKED, USER_EMAIL_EXISTS)
type Definition ¶
type Definition struct {
// contains filtered or unexported fields
}
Definition is a pre-defined (static) error. Implements AppError and error — can be returned directly as error. Each project defines its Definitions as package-level variables.
Simple usage (direct return):
return errcodes.ErrAccountBlocked
With context (creates *Error):
return errcodes.ErrLoginFailed.WithMeta("remaining_attempts", 2)
return errcodes.ErrInternal.Wrap(err)
func Define ¶
func Define(kind Kind, code Code, message string) *Definition
Define creates an error Definition. Called once at package initialization.
Example:
var ErrAccountBlocked = apperr.Define(apperr.KindAuthentication, "AUTH_ACCOUNT_BLOCKED", "account blocked due to failed login attempts")
func (*Definition) Code ¶
func (d *Definition) Code() Code
func (*Definition) Error ¶
func (d *Definition) Error() string
Error implements the error interface — allows direct return as error.
func (*Definition) Is ¶
func (d *Definition) Is(target error) bool
Is allows comparison with errors.Is() — compares by Code. Works with both *Error and *Definition as target.
func (*Definition) Kind ¶
func (d *Definition) Kind() Kind
func (*Definition) Message ¶
func (d *Definition) Message() string
func (*Definition) WithMessage ¶
func (d *Definition) WithMessage(message string) *Error
WithMessage creates an *Error with a custom message (overrides the default). Useful when the message needs dynamic context.
func (*Definition) WithMeta ¶
func (d *Definition) WithMeta(key string, value any) *Error
WithMeta creates an *Error with metadata. Used for dynamic data that the frontend needs (e.g., remaining_attempts, field_name).
func (*Definition) Wrap ¶
func (d *Definition) Wrap(cause error) *Error
Wrap creates an *Error from this Definition, wrapping an original error. Use when you want to preserve the original error for logging.
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is a domain error with context (cause, meta). Created when extra information needs to be added to a Definition, via WithMeta, Wrap, or WithMessage. Implements AppError and Go's error interface.
func (*Error) Is ¶
Is allows comparison with errors.Is() — compares by Code. Works with both *Error and *Definition as target.
type Kind ¶
type Kind uint8
Kind represents the category of an application error. It is transport-agnostic — the transport layer maps it to HTTP/gRPC/etc.
const ( KindValidation Kind = iota + 1 // input or business rule violation KindNotFound // resource not found KindConflict // duplicate, constraint violation KindAuthentication // invalid credentials, expired token KindAuthorization // no permission for the resource KindInternal // unexpected system error KindRateLimited // rate limit exceeded )