]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
wrap before/after_cursor_execute event hooks in error handling
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 26 Jun 2026 14:09:38 +0000 (10:09 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 26 Jun 2026 14:58:00 +0000 (10:58 -0400)
commit295013ba17895cb925bef042b9ad70c80f1d19b0
tree85663d92a055aa67d67f41bbb4fde7d57892a80a
parent101fd7f9b31db22f6ca2efdd3384a1e2751890bd
wrap before/after_cursor_execute event hooks in error handling

Expanded try/except error handling in _exec_single_context(),
_exec_insertmany_context(), and _cursor_execute() to encompass the
before_cursor_execute and after_cursor_execute event hooks. This
ensures that exceptions raised within these hooks, including
BaseException subclasses such as asyncio.CancelledError, are
properly handled via _handle_dbapi_exception(), providing correct
connection invalidation and pool notification.

Also added a guard in _handle_dbapi_exception to avoid
double-wrapping exceptions that are already StatementError
instances, which could occur when _cursor_execute's error handling
propagates up through _execute_context.

As part of this change, DBAPI errors raised from within these event
hooks will now be wrapped as SQLAlchemy exceptions.

Fixes: #13381
Change-Id: I6df29406f1eed9c318d02b00b999408c1e83535d
doc/build/changelog/unreleased_21/13381.rst [new file with mode: 0644]
lib/sqlalchemy/engine/base.py
test/engine/test_execute.py
test/ext/asyncio/test_engine.py