errors

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustvalidateCodes

func MustvalidateCodes()

Types

type Code

type Code string
const (
	// --- Database / Storage Errors ---
	CodeDBConnection      Code = "db.connection_failed"
	CodeDBQueryFailed     Code = "db.query_failed"
	CodeDBMigrationFailed Code = "db.migration_failed"
	CodeCacheUnavailable  Code = "cache.unavailable"
	CodeDBTimeout         Code = "db.timeout"

	// --- Request / Validation Errors ---
	CodeInvalidRequest   Code = "request.invalid"
	CodeMissingField     Code = "request.missing_field"
	CodeInvalidField     Code = "request.invalid_field"
	CodePayloadTooLarge  Code = "request.payload_too_large"
	CodeUnsupportedMedia Code = "request.unsupported_media"

	// --- Authentication / Authorization ---
	CodeUnauthorized Code = "auth.unauthorized"
	CodeForbidden    Code = "auth.forbidden"
	CodeTokenExpired Code = "auth.token_expired"
	CodeTokenInvalid Code = "auth.token_invalid"
	CodeUserDisabled Code = "auth.user_disabled"

	// --- Resource / Object Errors ---
	CodeNotFound          Code = "resource.not_found"
	CodeAlreadyExists     Code = "resource.already_exists"
	CodeConflict          Code = "resource.conflict"
	CodeResourceLocked    Code = "resource.locked"
	CodeDependencyMissing Code = "resource.dependency_missing"

	// --- Rate Limiting / Throttling ---
	CodeRateLimit       Code = "rate_limited"
	CodeTooManyRequests Code = "rate.too_many_requests"

	// --- Internal / Unexpected Errors ---
	CodeInternalError  Code = "internal.error"
	CodePanicRecovered Code = "internal.panic_recovered"
	CodeEncodingFailed Code = "internal.encoding_failed"
	CodeUnknown        Code = "internal.unknown"

	// --- External / 3rd Party Services ---
	CodeExternalAPIError    Code = "external.api_error"
	CodeExternalTimeout     Code = "external.timeout"
	CodeExternalUnreachable Code = "external.unreachable"
	CodeExternalBadResponse Code = "external.bad_response"

	// --- Observability / Traceability ---
	CodeTraceMissing     Code = "observability.trace_missing"
	CodeMetricsCorrupted Code = "observability.metrics_corrupted"
	CodeLogFormatInvalid Code = "observability.log_format_invalid"

	// --- Feature Flags / Config ---
	CodeFeatureDisabled Code = "feature.disabled"
	CodeInvalidConfig   Code = "config.invalid"
	CodeConfigMissing   Code = "config.missing"
)

type Error

type Error struct {
	Code     Code           `json:"code"`
	Message  string         `json:"message"`
	Severity Severity       `json:"severity"`
	Cause    error          `json:"-"`
	Meta     map[string]any `json:"meta,omitempty"`
}

func New

func New(code Code, message string, severity Severity, cause ...error) *Error

New creates a new structured error

func NewMeta

func NewMeta(code Code, message string, severity Severity, meta map[string]any, cause ...error) *Error

NewMeta includes structured meta data

func Wrap

func Wrap(err error, code Code, message string, severity Severity) *Error

func (*Error) Error

func (e *Error) Error() string

Error interface compatibility

func (*Error) LogValue

func (e *Error) LogValue() slog.Value

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap for 'errors.Is/As'

type Severity

type Severity string
const (
	SeverityInfo     Severity = "info"     // harmless events, e.g. optional config fallback
	SeverityWarning  Severity = "warning"  // recoverable issues, e.g. degraded service
	SeverityError    Severity = "error"    // user-impacting or failed operations
	SeverityCritical Severity = "critical" // system-wide issues needing urgent attention
)

Jump to

Keyboard shortcuts

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