errors

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = New(CodeNotFound, "record not found")

ErrNotFound is a convenience sentinel for not-found errors.

View Source
var ErrUniqueViolation = New(CodeUniqueViolation, "unique constraint violated")

ErrUniqueViolation is a convenience sentinel for unique constraint violations.

Functions

func Is

func Is(err error, code ErrorCode) bool

Is checks if the error matches a specific error code. It walks the error chain using errors.As to find a *DBError and compares its Code field.

func IsConnectionError

func IsConnectionError(err error) bool

IsConnectionError reports whether err is a DBError with CodeConnectionError.

func IsForeignKeyViolation

func IsForeignKeyViolation(err error) bool

IsForeignKeyViolation reports whether err is a DBError with CodeForeignKeyViolation.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err is a DBError with CodeNotFound.

func IsTimeout

func IsTimeout(err error) bool

IsTimeout reports whether err is a DBError with CodeTimeout.

func IsUniqueViolation

func IsUniqueViolation(err error) bool

IsUniqueViolation reports whether err is a DBError with CodeUniqueViolation.

Types

type DBError

type DBError struct {
	// Code is the classified error category.
	Code ErrorCode

	// Message is a user-friendly error message.
	Message string

	// Detail provides additional diagnostic context.
	Detail string

	// Cause is the original error from the database driver.
	Cause error

	// Meta holds additional metadata (e.g., constraint name, table, column).
	Meta map[string]string
}

DBError is the unified ORM error type that wraps raw database errors with classification, context, and user-friendly messaging.

func AsDBError

func AsDBError(err error) (*DBError, bool)

AsDBError attempts to extract a *DBError from an error chain.

func New

func New(code ErrorCode, message string) *DBError

New creates a new DBError with the given code and message.

func Wrap

func Wrap(cause error, code ErrorCode, message string) *DBError

Wrap wraps a raw error with classification.

func (*DBError) Error

func (e *DBError) Error() string

Error returns a formatted error string including the code, message, detail (if present), and cause (if present).

func (*DBError) Unwrap

func (e *DBError) Unwrap() error

Unwrap returns the underlying cause, supporting errors.Unwrap.

func (*DBError) WithDetail

func (e *DBError) WithDetail(detail string) *DBError

WithDetail returns a copy of the error with a detail message.

func (*DBError) WithMeta

func (e *DBError) WithMeta(key, value string) *DBError

WithMeta returns a copy of the error with additional metadata.

type ErrorCode

type ErrorCode int

ErrorCode represents a classified database error category.

const (
	CodeUnknown ErrorCode = iota
	CodeUniqueViolation
	CodeForeignKeyViolation
	CodeNotFound
	CodeSerializationFailure
	CodeDeadlock
	CodeConnectionError
	CodeTimeout
	CodeMigrationDrift
	CodeInvalidSchema
	CodeUnsupportedFeature
	CodeCheckViolation
	CodeNotNullViolation
	CodeDataTruncation
)

func (ErrorCode) String

func (c ErrorCode) String() string

String returns the human-readable name of the error code.

Jump to

Keyboard shortcuts

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