From c60eb86a91eac57e556c07ee2a34870c065a9830 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 3 Jul 2014 21:49:10 -0400 Subject: [PATCH] - Added new attributes :attr:`.ExecutionContext.exception` and :attr:`.ExecutionContext.is_disconnect` which are meaningful within the :meth:`.ConnectionEvents.dbapi_error` handler to see both the original DBAPI error as well as whether or not it represents a disconnect. --- doc/build/changelog/changelog_09.rst | 10 ++++++++ lib/sqlalchemy/engine/base.py | 8 ++++++- lib/sqlalchemy/engine/interfaces.py | 35 ++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst index 074dd6e316..d3bce5cbee 100644 --- a/doc/build/changelog/changelog_09.rst +++ b/doc/build/changelog/changelog_09.rst @@ -24,6 +24,16 @@ of raising or returning a new exception object, which will replace the exception normally being thrown by SQLAlchemy. + .. change:: + :tags: feature, engine + :versions: 1.0.0 + + Added new attributes :attr:`.ExecutionContext.exception` and + :attr:`.ExecutionContext.is_disconnect` which are meaningful within + the :meth:`.ConnectionEvents.dbapi_error` handler to see both the + original DBAPI error as well as whether or not it represents + a disconnect. + .. change:: :tags: bug, orm :tickets: 3108 diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index c885bcf69e..67772f131e 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -1070,10 +1070,16 @@ class Connection(Connectable): exc_info = sys.exc_info() + if context and context.exception is None: + context.exception = e + if not self._is_disconnect: - self._is_disconnect = isinstance(e, self.dialect.dbapi.Error) and \ + self._is_disconnect = \ + isinstance(e, self.dialect.dbapi.Error) and \ not self.closed and \ self.dialect.is_disconnect(e, self.__connection, cursor) + if context: + context.is_disconnect = self._is_disconnect if self._reentrant_error: util.raise_from_cause( diff --git a/lib/sqlalchemy/engine/interfaces.py b/lib/sqlalchemy/engine/interfaces.py index 22801c2843..230d00fc01 100644 --- a/lib/sqlalchemy/engine/interfaces.py +++ b/lib/sqlalchemy/engine/interfaces.py @@ -707,6 +707,41 @@ class ExecutionContext(object): and updates. """ + exception = None + """A DBAPI-level exception that was caught when this ExecutionContext + attempted to execute a statement. + + This attribute is meaningful only within the + :meth:`.ConnectionEvents.dbapi_error` event. + + .. versionadded:: 0.9.7 + + .. seealso:: + + :attr:`.ExecutionContext.is_disconnect` + + :meth:`.ConnectionEvents.dbapi_error` + + """ + + is_disconnect = None + """Boolean flag set to True or False when a DBAPI-level exception + is caught when this ExecutionContext attempted to execute a statement. + + This attribute is meaningful only within the + :meth:`.ConnectionEvents.dbapi_error` event. + + .. versionadded:: 0.9.7 + + .. seealso:: + + :attr:`.ExecutionContext.exception` + + :meth:`.ConnectionEvents.dbapi_error` + + """ + + def create_cursor(self): """Return a new cursor generated from this ExecutionContext's connection. -- 2.47.3