diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-07-03 17:30:49 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-07-03 17:30:49 -0400 |
commit | e0a9b94abb92c6b62d6a6f70dec680d7ca35eed6 (patch) | |
tree | 266b183872d635d9adb6a5b3281666941b9c3978 /lib/sqlalchemy/engine/base.py | |
parent | 3e4286079c760e9f8e3e76278b2a0c4d406a230d (diff) | |
download | sqlalchemy-e0a9b94abb92c6b62d6a6f70dec680d7ca35eed6.tar.gz |
- The mechanics of the :meth:`.ConnectionEvents.dbapi_error` handler
have been enhanced such that the function handler is now capable
of raising or returning a new exception object, which will replace
the exception normally being thrown by SQLAlchemy.
fixes #3076
Diffstat (limited to 'lib/sqlalchemy/engine/base.py')
-rw-r--r-- | lib/sqlalchemy/engine/base.py | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 249c494fe..c885bcf69 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -1090,14 +1090,28 @@ class Connection(Connectable): should_wrap = isinstance(e, self.dialect.dbapi.Error) or \ (statement is not None and context is None) + newraise = None if should_wrap and context: if self._has_events or self.engine._has_events: - self.dispatch.dbapi_error(self, - cursor, - statement, - parameters, - context, - e) + for fn in self.dispatch.dbapi_error: + try: + # handler returns an exception; + # call next handler in a chain + per_fn = fn(self, + cursor, + statement, + parameters, + context, + newraise + if newraise + is not None else e) + if per_fn is not None: + newraise = per_fn + except Exception as _raised: + # handler raises an exception - stop processing + newraise = _raised + + context.handle_dbapi_exception(e) if not self._is_disconnect: @@ -1105,7 +1119,9 @@ class Connection(Connectable): self._safe_close_cursor(cursor) self._autorollback() - if should_wrap: + if newraise: + util.raise_from_cause(newraise, exc_info) + elif should_wrap: util.raise_from_cause( exc.DBAPIError.instance( statement, |