errors

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package errors provides safe application errors interoperable with standard Go errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

As delegates standard error type matching.

func Is

func Is(err, target error) bool

Is delegates standard error identity matching.

func Recover

func Recover(dst *error, report func(error))

Recover converts a panic into an error and invokes report. Use as defer Recover(&err, report).

func Unwrap

func Unwrap(err error) error

Unwrap returns the underlying wrapped resource or error cause.

Types

type AppError

type AppError struct {
	Code          string         `json:"code"`
	Message       string         `json:"message"`
	DebugMessage  string         `json:"-"`
	Category      Category       `json:"category"`
	Severity      Severity       `json:"severity"`
	HTTPStatus    int            `json:"-"`
	Details       map[string]any `json:"details,omitempty"`
	Cause         error          `json:"-"`
	Retryable     bool           `json:"retryable,omitempty"`
	RetryAfter    time.Duration  `json:"-"`
	TraceID       string         `json:"trace_id,omitempty"`
	CorrelationID string         `json:"correlation_id,omitempty"`
	Metadata      map[string]any `json:"metadata,omitempty"`
}

AppError separates public data from private diagnostics. Details and Metadata must contain only caller-approved public data.

func Classify

func Classify(err error) *AppError

Classify recognizes core errors without importing optional drivers.

func New

func New(message string, opts ...Option) *AppError

New constructs and validates an explicitly owned instance. See package documentation for defaults and cleanup.

func Wrap

func Wrap(cause error, message string, opts ...Option) *AppError

Wrap constructs an application error preserving its underlying cause.

func Wrapf

func Wrapf(cause error, format string, args ...any) *AppError

Wrapf constructs a wrapped error with a formatted customer-facing message.

func (*AppError) Error

func (e *AppError) Error() string

Error returns a diagnostic error description.

func (*AppError) Unwrap

func (e *AppError) Unwrap() error

Unwrap returns the underlying wrapped resource or error cause.

type Category

type Category string

Category classifies an application failure.

const (
	Validation     Category = "VALIDATION"
	Authentication Category = "AUTHENTICATION"
	Authorization  Category = "AUTHORIZATION"
	Business       Category = "BUSINESS"
	NotFound       Category = "NOT_FOUND"
	Conflict       Category = "CONFLICT"
	RateLimit      Category = "RATE_LIMIT"
	Database       Category = "DATABASE"
	Cache          Category = "CACHE"
	Messaging      Category = "MESSAGING"
	Network        Category = "NETWORK"
	Infrastructure Category = "INFRASTRUCTURE"
	Internal       Category = "INTERNAL"
	Timeout        Category = "TIMEOUT"
	Unavailable    Category = "UNAVAILABLE"
)

type Option

type Option func(*AppError)

Option configures a newly constructed application error.

func WithCategory

func WithCategory(v Category) Option

WithCategory sets the application error category.

func WithCause

func WithCause(v error) Option

WithCause sets the underlying diagnostic cause.

func WithCode

func WithCode(v string) Option

WithCode sets a machine-readable application error code.

func WithDebug

func WithDebug(v string) Option

WithDebug sets internal diagnostic text excluded from default responses.

func WithDetails

func WithDetails(v map[string]any) Option

WithDetails sets explicitly approved public validation details.

func WithHTTPStatus

func WithHTTPStatus(v int) Option

WithHTTPStatus sets the intended HTTP status.

func WithMessage

func WithMessage(v string) Option

WithMessage sets an approved customer-facing message.

func WithRetryAfter

func WithRetryAfter(v time.Duration) Option

WithRetryAfter sets retry delay metadata.

func WithRetryable

func WithRetryable(v bool) Option

WithRetryable sets retryability metadata without performing retries.

func WithSeverity

func WithSeverity(v Severity) Option

WithSeverity sets the application error severity.

type Severity

type Severity string

Severity indicates diagnostic importance.

const (
	Debug    Severity = "DEBUG"
	Info     Severity = "INFO"
	Warning  Severity = "WARNING"
	Error    Severity = "ERROR"
	Critical Severity = "CRITICAL"
)

Jump to

Keyboard shortcuts

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