Skip to content
Browse files

Fixed #17601 -- expose underlying db exceptions under py2

Use __cause__ to expose the underlying database exceptions even
under python 2.
  • Loading branch information...
1 parent 4f4e924 commit 54485557855d58d9a5027511025d5ab22f721c6d @jaylett jaylett committed
Showing with 8 additions and 3 deletions.
  1. +1 −2 django/db/
  2. +7 −1 docs/ref/exceptions.txt
3 django/db/
@@ -91,8 +91,7 @@ def __exit__(self, exc_type, exc_value, traceback):
except AttributeError:
args = (exc_value,)
dj_exc_value = dj_exc_type(*args)
- if six.PY3:
- dj_exc_value.__cause__ = exc_value
+ dj_exc_value.__cause__ = exc_value
# Only set the 'errors_occurred' flag for errors that may make
# the connection unusable.
if dj_exc_type not in (DataError, IntegrityError):
8 docs/ref/exceptions.txt
@@ -152,10 +152,16 @@ The Django wrappers for database exceptions behave exactly the same as
the underlying database exceptions. See :pep:`249`, the Python Database API
Specification v2.0, for further information.
+As per :pep:`3134`, a ``__cause__`` attribute is set with the original
+(underlying) database exception, allowing access to any additional
+information provided. (Note that this attribute is available under
+both Python 2 and Python 3, although :pep:`3134` normally only applies
+to Python 3.)
.. versionchanged:: 1.6
Previous version of Django only wrapped ``DatabaseError`` and
- ``IntegrityError``.
+ ``IntegrityError``, and did not provide ``__cause__``.
.. exception:: models.ProtectedError

0 comments on commit 5448555

Please sign in to comment.
Something went wrong with that request. Please try again.