summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/build/changelog/changelog_09.rst10
-rw-r--r--lib/sqlalchemy/engine/base.py8
-rw-r--r--lib/sqlalchemy/engine/interfaces.py35
3 files changed, 52 insertions, 1 deletions
diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst
index 074dd6e31..d3bce5cbe 100644
--- a/doc/build/changelog/changelog_09.rst
+++ b/doc/build/changelog/changelog_09.rst
@@ -25,6 +25,16 @@
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
:versions: 1.0.0
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py
index c885bcf69..67772f131 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 22801c284..230d00fc0 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.