Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions piccolo/engine/exceptions.py
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions piccolo/engine/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = (
Expand All @@ -263,6 +269,7 @@ class PostgresEngine(Engine):
"extra_nodes",
"pool",
"transaction_connection",
"use_generic_exceptions",
)

engine_type = "postgres"
Expand All @@ -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
Expand Down