Exceptions#

Exception hierarchy and SQLAlchemy error translation utilities converting low-level driver errors such as sqlalchemy.exc.IntegrityError and sqlalchemy.exc.OperationalError into typed domain exceptions.

Base & Configuration Exceptions#

exception advanced_alchemy.exceptions.AdvancedAlchemyError(*args, detail='')[source]#

Bases: Exception

Base exception class from which all Advanced Alchemy exceptions inherit.

exception advanced_alchemy.exceptions.ImproperConfigurationError[source]#

Bases: AdvancedAlchemyError

Improper Configuration error.

This exception is raised when there is an issue with the configuration of a module.

Parameters:
  • *args (Any) – Variable length argument list passed to parent class.

  • detail (str) – Detailed error message.

exception advanced_alchemy.exceptions.MissingDependencyError(package, install_package=None)[source]#

Bases: AdvancedAlchemyError, ImportError

Missing optional dependency.

This exception is raised when a module depends on a dependency that has not been installed.

Parameters:
  • package (str) – Name of the missing package.

  • install_package (str | None) – Optional alternative package name to install.

exception advanced_alchemy.exceptions.SerializationError[source]#

Bases: AdvancedAlchemyError

Encoding or decoding error.

This exception is raised when serialization or deserialization of an object fails.

Parameters:
  • *args (Any) – Variable length argument list passed to parent class.

  • detail (str) – Detailed error message.

Repository & Integrity Exceptions#

exception advanced_alchemy.exceptions.RepositoryError[source]#

Bases: AdvancedAlchemyError

Base repository exception type.

Parameters:
  • *args (Any) – Variable length argument list passed to parent class.

  • detail (str) – Detailed error message.

exception advanced_alchemy.exceptions.IntegrityError[source]#

Bases: RepositoryError

Data integrity error.

Parameters:
  • *args (Any) – Variable length argument list passed to parent class.

  • detail (str) – Detailed error message.

exception advanced_alchemy.exceptions.DuplicateKeyError[source]#

Bases: IntegrityError

Duplicate key error.

Parameters:
  • *args (Any) – Variable length argument list passed to parent class.

  • detail (str) – Detailed error message.

exception advanced_alchemy.exceptions.ForeignKeyError[source]#

Bases: IntegrityError

Foreign key error.

Parameters:
  • *args (Any) – Variable length argument list passed to parent class.

  • detail (str) – Detailed error message.

exception advanced_alchemy.exceptions.NotFoundError[source]#

Bases: RepositoryError

Not found error.

This exception is raised when a requested resource is not found.

Parameters:
  • *args (Any) – Variable length argument list passed to parent class.

  • detail (str) – Detailed error message.

exception advanced_alchemy.exceptions.MultipleResultsFoundError[source]#

Bases: RepositoryError

Multiple results found error.

This exception is raised when a single result was expected but multiple were found.

Parameters:
  • *args (Any) – Variable length argument list passed to parent class.

  • detail (str) – Detailed error message.

exception advanced_alchemy.exceptions.InvalidRequestError[source]#

Bases: RepositoryError

Invalid request error.

This exception is raised when SQLAlchemy is unable to complete the request due to a runtime error

Parameters:
  • *args (Any) – Variable length argument list passed to parent class.

  • detail (str) – Detailed error message.

Exception Translation & Error Templates#

class advanced_alchemy.exceptions.ErrorMessages[source]#

Bases: TypedDict

Custom error message templates or callables keyed by error category.

duplicate_key: str | Callable[[Exception], str]#

Error message or callable for duplicate key constraint violations.

integrity: str | Callable[[Exception], str]#

Error message or callable for general integrity constraint violations.

foreign_key: str | Callable[[Exception], str]#

Error message or callable for foreign key constraint violations.

multiple_rows: str | Callable[[Exception], str]#

Error message or callable for queries returning multiple rows when one was expected.

check_constraint: str | Callable[[Exception], str]#

Error message or callable for check constraint violations.

other: str | Callable[[Exception], str]#

Error message or callable for unclassified repository exceptions.

not_found: str | Callable[[Exception], str]#

Error message or callable when no matching row is found.

advanced_alchemy.exceptions.wrap_sqlalchemy_exception(
error_messages=None,
dialect_name=None,
wrap_exceptions=True,
)[source]#

Do something within context to raise a RepositoryError chained from an original SQLAlchemyError.

>>> try:
...     with wrap_sqlalchemy_exception():
...         raise SQLAlchemyError("Original Exception")
... except RepositoryError as exc:
...     print(
...         f"caught repository exception from {type(exc.__context__)}"
...     )
caught repository exception from <class 'sqlalchemy.exc.SQLAlchemyError'>
Parameters:
  • error_messages (ErrorMessages | None) – Error messages to use for the exception.

  • dialect_name (str | None) – The name of the dialect to use for the exception.

  • wrap_exceptions (bool) – Wrap SQLAlchemy exceptions in a RepositoryError. When set to False, the original exception will be raised.

Raises:
  • NotFoundError – Raised when no rows matched the specified data.

  • MultipleResultsFound – Raised when multiple rows matched the specified data.

  • IntegrityError – Raised when an integrity error occurs.

  • InvalidRequestError – Raised when an invalid request was made to SQLAlchemy.

  • RepositoryError – Raised for other SQLAlchemy errors.

  • AttributeError – Raised when an attribute error occurs during processing.

  • SQLAlchemyError – Raised for general SQLAlchemy errors.

  • StatementError – Raised when there is an issue processing the statement.

  • MultipleResultsFoundError – Raised when multiple rows matched the specified data.

Return type:

Generator[None, None, None]