Exceptions¶
from sqlspec.exceptions import (
CheckViolationError,
DatabaseConnectionError,
DataError,
DeadlockError,
ForeignKeyViolationError,
ImproperConfigurationError,
IntegrityError,
MigrationError,
MissingDependencyError,
MultipleResultsFoundError,
NotFoundError,
NotNullViolationError,
OperationalError,
PermissionDeniedError,
QueryTimeoutError,
SerializationConflictError,
SQLSpecError,
StackExecutionError,
TransactionError,
UniqueViolationError,
map_sqlstate_to_exception,
)
Normalized exception hierarchy raised across all 20 database drivers, query builders, SQL file loaders, migrations, and storage pipelines.
Base & Configuration Exceptions¶
ImproperConfigurationError
¶
ConfigResolverError
¶
MissingDependencyError
¶
DialectNotSupportedError
¶
Integrity & Constraint Violations¶
IntegrityError
¶
UniqueViolationError
¶
ForeignKeyViolationError
¶
NotNullViolationError
¶
CheckViolationError
¶
Execution, Concurrency & Operational Errors¶
OperationalError
¶
DatabaseConnectionError
¶
ConnectionTimeoutError
¶
Bases: DatabaseConnectionError
Database connection attempt timed out.
Raised when
- TCP connection timeout to database server
- DNS resolution timeout
- SSL/TLS handshake timeout
- Oracle connect timeout (ORA-12170)
QueryTimeoutError
¶
Bases: OperationalError
Query execution exceeded a timeout or deadline.
Raised when
- Lock wait timeout exceeded (MySQL 1205)
- Statement or service deadline exceeded
OperationCancelledError
¶
PermissionDeniedError
¶
Bases: DatabaseConnectionError
Database access denied due to insufficient privileges.
Raised when
- User lacks privileges for the operation (SQLSTATE 42501)
- Invalid credentials provided (SQLSTATE 28000/28P01)
- Database access denied (MySQL 1044/1045/1142)
- Oracle insufficient privileges (ORA-01031)
TransactionError
¶
SerializationConflictError
¶
DeadlockError
¶
Bases: TransactionError
Deadlock detected during transaction execution.
Raised when
- PostgreSQL deadlock detected (SQLSTATE 40P01)
- MySQL deadlock detected (Error 1213)
- Oracle deadlock detected (ORA-00060)
- SQLite database locked (SQLITE_LOCKED)
Applications should typically retry the transaction when this error occurs.
DataError
¶
InvalidCursorError
¶
NotFoundError
¶
MultipleResultsFoundError
¶
RepositoryError
¶
StackExecutionError
¶
SQL Parsing, File Loader, Migration & Storage Errors¶
SQLParsingError
¶
SQLConversionError
¶
SQLBuilderError
¶
SQLFileNotFoundError
¶
SQLFileParseError
¶
SQLStatementNotFoundError
¶
SQLFragmentNotFoundError
¶
Bases: SQLStatementNotFoundError
Raised when a SQL fragment named by an include or lookup is not loaded.
SQLSlotError
¶
MigrationError
¶
OutOfOrderMigrationError
¶
Bases: MigrationError
Raised when an out-of-order migration is detected in strict mode.
Out-of-order migrations occur when a pending migration has a timestamp earlier than already-applied migrations, typically from late-merging branches.
SquashValidationError
¶
Bases: MigrationError
Raised when migration squash validation fails.
Squash validation errors occur when
- Version range is invalid (start > end)
- Gap detected in version sequence
- Mixed migration types that cannot be squashed
- Target file already exists
StorageOperationFailedError
¶
FileNotFoundInStorageError
¶
Bases: StorageOperationFailedError
Raised when a file or object is not found in the storage backend.
StorageCapabilityError
¶
StoragePathTraversalError
¶
Bases: StorageOperationFailedError
Raised when a storage path would resolve outside its backend root.
EventChannelError
¶
SQLSTATE Mapping Helper¶
SQLSTATE_EXCEPTION_MAP maps standard 5-character ANSI/PostgreSQL SQLSTATE codes ("23505", "40001", "40P01", "57014", etc.) to their corresponding SQLSpecError subclass.
map_sqlstate_to_exception
¶
Map a SQLSTATE code to a SQLSpec exception class.
Checks in order of specificity
- Exact 5-character match
- 2-character class match
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sqlstate
|
str | None
|
5-character SQLSTATE code |
required |
Returns:
| Type | Description |
|---|---|
type[SQLSpecError] | None
|
Matching exception class or None if not mapped |