Opened 3 years ago

Closed 3 years ago

#18292 closed New feature (duplicate)

Database errors get mangled

Reported by: gcbirzan@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When using the Django wrapper around the database connection, all errors are mangled into two exceptions, DatabaseError and IntegrityError. The only trace of the original exception is in the message, which can be localised, and is not always reliable anyway.

This seems to be a design decision as instead of raising the correct exception, a DatabaseError is constructed with the same message: https://code.djangoproject.com/browser/django/trunk/django/db/backends/postgresql_psycopg2/base.py#L56

This makes it non-trivial (bordering on impossible) to do some database stuff through the default django db backends.

Change History (2)

comment:1 Changed 3 years ago by akaariai

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Bug to New feature

The only solution would be to add the original exception as a wrapped in Django's exception. Using this you could get the original exception by something like:

try:
    do_something()
except django.db.utils.IntegrityError as e:
    if isinstance(e.wrapped, ...

I can see the use of that. Not as nice as getting the real raw exception, but then again that can't be provided while keeping compatibility and unified database interface...

comment:2 Changed 3 years ago by carljm

  • Resolution set to duplicate
  • Status changed from new to closed

Dupe of #15901

Note: See TracTickets for help on using tickets.
Back to Top