Documentation
¶
Overview ¶
Package dberrors contains errors related to database operations.
Index ¶
- Variables
- func HandleError(err error) (int, error)
- func UserFriendlyError(err error) (int, error)
- func WrapError(operation, resource string, err error) error
- type DBError
- func AlreadyExists(resource string) *DBError
- func ConnectionError(database string, err error) *DBError
- func InvalidInput(resource, details string) *DBError
- func NewDBError(operation, resource string, err error) *DBError
- func NotFound(resource string) *DBError
- func TransactionError(operation string, err error) *DBError
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
UserFriendlyError converts a database error into an HTTP status code and user-friendly error
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 ¶
AlreadyExists creates an "already exists" error for a specific resource
func ConnectionError ¶
ConnectionError creates a connection failure error
func InvalidInput ¶
InvalidInput creates an "invalid input" error with details
func NewDBError ¶
NewDBError creates a new database error with context
func TransactionError ¶
TransactionError creates a transaction failure error
func (*DBError) WithDetails ¶
WithDetails adds developer details to the error
func (*DBError) WithHTTPStatus ¶
WithHTTPStatus sets the HTTP status code
func (*DBError) WithUserMessage ¶
WithUserMessage sets a user-friendly message