Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit exception chaining for db exceptions by setting __cause__ in py2 #1241

Merged
merged 1 commit into from
Jun 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions django/db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 7 additions & 1 deletion docs/ref/exceptions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down