dberrors

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package dberrors contains errors related to database operations.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound            = errors.New("resource not found")
	ErrAlreadyExists       = errors.New("resource already exists")
	ErrInvalidInput        = errors.New("invalid input provided")
	ErrConnectionFailed    = errors.New("database connection failed")
	ErrTimeout             = errors.New("database operation timed out")
	ErrConstraintViolation = errors.New("database constraint violation")
	ErrForeignKeyViolation = errors.New("foreign key constraint violation")
	ErrUniqueViolation     = errors.New("unique constraint violation")
	ErrTransactionFailed   = errors.New("transaction failed")
	ErrInvalidOperation    = errors.New("invalid database operation")
)

Common database errors

Functions

func HandleError

func HandleError(err error) (int, error)

HandleError is the main entry point for error handling. Pass any error and get back a status code and user-friendly error message. It automatically detects database errors and provides better messages.

Usage:

status, err := dberrors.HandleError(someError)
http.Error(w, err.Error(), status)

func UserFriendlyError

func UserFriendlyError(err error) (int, error)

UserFriendlyError converts a database error into an HTTP status code and user-friendly error

func WrapError

func WrapError(operation, resource string, err error) error

WrapError wraps an error with database operation context

Types

type DBError

type DBError struct {
	Operation   string // e.g., "insert", "update", "delete", "select"
	Resource    string // e.g., "user", "table", "database"
	Err         error  // underlying error
	Details     string // additional details for developers
	UserMessage string // user-friendly message
	HTTPStatus  int    // HTTP status code
}

DBError represents a detailed database error with context

func AlreadyExists

func AlreadyExists(resource string) *DBError

AlreadyExists creates an "already exists" error for a specific resource

func ConnectionError

func ConnectionError(database string, err error) *DBError

ConnectionError creates a connection failure error

func InvalidInput

func InvalidInput(resource, details string) *DBError

InvalidInput creates an "invalid input" error with details

func NewDBError

func NewDBError(operation, resource string, err error) *DBError

NewDBError creates a new database error with context

func NotFound

func NotFound(resource string) *DBError

NotFound creates a "not found" error for a specific resource

func TransactionError

func TransactionError(operation string, err error) *DBError

TransactionError creates a transaction failure error

func (*DBError) Error

func (e *DBError) Error() string

func (*DBError) Unwrap

func (e *DBError) Unwrap() error

func (*DBError) WithDetails

func (e *DBError) WithDetails(details string) *DBError

WithDetails adds developer details to the error

func (*DBError) WithHTTPStatus

func (e *DBError) WithHTTPStatus(status int) *DBError

WithHTTPStatus sets the HTTP status code

func (*DBError) WithUserMessage

func (e *DBError) WithUserMessage(msg string) *DBError

WithUserMessage sets a user-friendly message

Jump to

Keyboard shortcuts

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