[Answer]-What fields available in DatabaseError? (django/python)

1👍

You can use the dir() built in to see all the attributes of an object.

>>> from django.db.utils import DatabaseError
>>> dir(DatabaseError)
['__class__',
 '__delattr__',
 '__dict__',
 '__doc__',
 '__format__',
 '__getattribute__',
 '__getitem__',
 '__getslice__',
 '__hash__',
 '__init__',
 '__module__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__setstate__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__unicode__',
 '__weakref__',
 'args',
 'message']

As you can see, you might find something useful in the args or message attributes. To investigate what these attributes return, you should catch the exception with a DatabaseError, print out the attributes we’re interest in and then invoke the python interactive debugger:

try:
    # whatever code is raising your exception
except DatabaseError as e:
    print e.args
    print e.message
    import pdb; pdb.set_trace()
    # then look at the output to see if you can find something useful

To understand where the args attribute comes from, we need to look at the BaseException docs which say

args –
The tuple of arguments given to the exception constructor. Some
built-in exceptions (like IOError) expect a certain number of
arguments and assign a special meaning to the elements of this tuple,
while others are usually called only with a single string giving an
error message

Hopefully there will be something of note in one of those attributes which you can check against.

Leave a comment