diff --git a/piccolo/engine/exceptions.py b/piccolo/engine/exceptions.py index 77d51c579..3a1e6bf50 100644 --- a/piccolo/engine/exceptions.py +++ b/piccolo/engine/exceptions.py @@ -1,2 +1,48 @@ class TransactionError(Exception): pass + + +############################################################################### +# These are generic database Exceptions as specified in PEP 249: +# https://peps.python.org/pep-0249/#exceptions + + +class Error(Exception): + pass + + +class InterfaceError(Error): + pass + + +class DatabaseError(Error): + pass + + +class DataError(DatabaseError): + pass + + +class OperationalError(DatabaseError): + pass + + +class IntegrityError(DatabaseError): + pass + + +class InternalError(DatabaseError): + pass + + +class ProgrammingError(DatabaseError): + pass + + +class NotSupportedError(DatabaseError): + pass + + +# TODO - write async context manager which maps these exceptions to generic +# exceptions: +# https://github.com/MagicStack/asyncpg/blob/master/asyncpg/exceptions/__init__.py diff --git a/piccolo/engine/postgres.py b/piccolo/engine/postgres.py index fb76714f0..a0efa73d8 100644 --- a/piccolo/engine/postgres.py +++ b/piccolo/engine/postgres.py @@ -254,6 +254,12 @@ class PostgresEngine(Engine): >>> await MyTable.select().run(node="read_replica_1") + :param use_generic_exceptions: + Each database driver raises different exceptions. If you want to make + your code portable across different databases (e.g. SQLite and + Postgres), set this to ``True``. All exceptions raised by the database + driver are converted to generic Piccolo exceptions. + """ # noqa: E501 __slots__ = ( @@ -263,6 +269,7 @@ class PostgresEngine(Engine): "extra_nodes", "pool", "transaction_connection", + "use_generic_exceptions", ) engine_type = "postgres" @@ -274,10 +281,12 @@ def __init__( extensions: t.Sequence[str] = ("uuid-ossp",), log_queries: bool = False, extra_nodes: t.Dict[str, PostgresEngine] = None, + use_generic_exceptions: bool = False, ) -> None: if extra_nodes is None: extra_nodes = {} + self.use_generic_exceptions = use_generic_exceptions self.config = config self.extensions = extensions self.log_queries = log_queries