Skip to content

errors

Translates native driver exceptions into the SQLAlchemy exceptions the SQLAlchemy execution path raises.

raise_translated(error, *, backend, sa_connection, sql, args, ismulti=False) async

Re-raise a driver exception as the sqlalchemy.exc exception SQLAlchemy would raise for it, so user code catches the same errors on both paths::

Text Only
1
2
3
asyncpg.UniqueViolationError
# -> sqlalchemy.exc.IntegrityError: (...UniqueViolationError) duplicate key
#    [SQL: INSERT INTO users ...] [parameters: (...)]

When the dialect recognises the error as a lost connection, the connection is invalidated first, so it is closed instead of going back to the pool for the next query to fail on.

Parameters:

Name Type Description Default
error BaseException

exception raised by the driver

required
backend NativeBackend

native backend the statement ran on

required
sa_connection AsyncConnection

connection the statement ran on

required
sql str

executed SQL

required
args Sequence[Any]

driver arguments of the statement (one list per row for batches)

required
ismulti bool

whether the statement ran once per row of a batch

False

Raises:

Type Description
sqlalchemy.exc.DBAPIError

always

Source code in ormar/databases/native/errors.py
Python
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
async def raise_translated(
    error: BaseException,
    *,
    backend: NativeBackend,
    sa_connection: AsyncConnection,
    sql: str,
    args: Sequence[Any],
    ismulti: bool = False,
) -> NoReturn:
    """
    Re-raise a driver exception as the ``sqlalchemy.exc`` exception SQLAlchemy
    would raise for it, so user code catches the same errors on both paths::

        asyncpg.UniqueViolationError
        # -> sqlalchemy.exc.IntegrityError: (...UniqueViolationError) duplicate key
        #    [SQL: INSERT INTO users ...] [parameters: (...)]

    When the dialect recognises the error as a lost connection, the
    connection is invalidated first, so it is closed instead of going back to
    the pool for the next query to fail on.

    :param error: exception raised by the driver
    :type error: BaseException
    :param backend: native backend the statement ran on
    :type backend: NativeBackend
    :param sa_connection: connection the statement ran on
    :type sa_connection: AsyncConnection
    :param sql: executed SQL
    :type sql: str
    :param args: driver arguments of the statement (one list per row for batches)
    :type args: Sequence[Any]
    :param ismulti: whether the statement ran once per row of a batch
    :type ismulti: bool
    :raises sqlalchemy.exc.DBAPIError: always
    """
    raw = await sa_connection.get_raw_connection()
    dbapi_connection = raw.dbapi_connection
    orig = backend.adapter.translate_error(
        error=error, dbapi_connection=dbapi_connection
    )
    invalidated = backend.dialect.is_disconnect(
        e=orig,  # type: ignore[arg-type]
        connection=dbapi_connection,
        cursor=None,
    )
    if invalidated:
        await sa_connection.invalidate()  # pragma: no cover
    raise exc.DBAPIError.instance(
        statement=sql,
        params=list(args),
        orig=orig,  # type: ignore[arg-type]
        dbapi_base_err=backend.dialect.loaded_dbapi.Error,
        hide_parameters=sa_connection.sync_engine.hide_parameters,
        connection_invalidated=invalidated,
        dialect=backend.dialect,
        ismulti=ismulti,
    ) from orig