Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - How to get debug info of DatabaseError "current transaction is aborted" in Django error page?

While developing on my Django project I'm getting a DatabaseError that says:

current transaction is aborted, commands ignored until end of transaction block

I know this happens because there was a bad PostgreSQL transaction without a rollback and the error in this bad transaction can be found in the PostgreSQL error log. However, I think it would be way more convienient if this error would be shown on the Django error page. Then you can directly see what's the problem.

So, is it possible to get these PostgreSQL errors on the Django error page? And if so, how?

like image 647
gitaarik Avatar asked Nov 04 '22 03:11

gitaarik


1 Answers

If you'd like to get down to the nitty-gritty details of the exception, then you can write a custom middleware class to wrap view processing and catch the unhandled django.db.DatabaseError and inspect the exception in a debug session from your development server's console:

from django.db import DatabaseError

class DatabaseErrorMiddleware(object):
    def process_view(self, request, view, args, kwargs):
       try:
           view(request, *args, **kwargs)   
       except DatabaseError as err:
           import pdb; pdb.set_trace()
           # You can now fully inspect the `e` exception object and it's context

These exceptions are benign and usually occur whenever a thread's request-response flow prematurely ends, quite often during development if your client aborts the connection before the application could return a response, though that might not be all of it.

like image 141
Filip Dupanović Avatar answered Nov 13 '22 18:11

Filip Dupanović