Login

Simple Exception Response for AJAX debugging

Author:
newmaniese
Posted:
March 19, 2008
Language:
Python
Version:
.96
Tags:
middleware ajax exception-handling
Score:
14 (after 14 ratings)

When debugging AJAX with Firebug, if a response is 500, it is a pain to have to view the entire source of the pretty exception page. This is a simple middleware that just returns the exception without any markup. You can add this anywhere in your python path and then put it in you settings file. It will only unprettify your exceptions when there is a XMLHttpRequest header. Tested in FF2 with the YUI XHR. Comments welcome.

EDIT: I recently changed the request checking to use the is_ajax() method. This gives you access to these simple exceptions for get requests as well (even though you could just point your browser there).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from django.conf import settings
from django.http import HttpResponseServerError

class AJAXSimpleExceptionResponse:
    def process_exception(self, request, exception):
        if settings.DEBUG:
            if request.is_ajax():
                import sys, traceback
                (exc_type, exc_info, tb) = sys.exc_info()
                response = "%s\n" % exc_type.__name__
                response += "%s\n\n" % exc_info
                response += "TRACEBACK:\n"    
                for tb in traceback.format_tb(tb):
                    response += "%s\n" % tb
                return HttpResponseServerError(response)

More like this

  1. Middleware for printing of exception to console by dballanc 7 years, 8 months ago
  2. View response's content in a browser while testing by ryankask1 4 years, 4 months ago
  3. another render_to_response wrapper by ro60 7 years, 5 months ago
  4. Super User Conditional Page Exception Reporting by zbyte64 6 years, 10 months ago
  5. Log all interaction with user to the DB by inuwashi 4 years, 4 months ago

Comments

luftyluft (on March 19, 2008):

Nice idea - thanks!

#

skabber (on March 19, 2008):

This will be great. Although I have gotten good at finding the exception in the source of the pretty error pages :)

#

robhudson (on March 20, 2008):

Works great. Thanks!

#

trevor (on March 23, 2008):

You can also document.write() on 500 in the javascript to display the error instead of trying to read it in firebug.

#

kioopi (on April 15, 2008):

I use it, too.

I use the Poster firefox add-on for a lot of Ajax-Debugging, but it seems buggy for HTTP_ACCEPT headers.

Thanks.

#

simonbun (on January 28, 2009):

I always detect in my Javascript if the response holds HTML. If it does, I create a floating iframe and pass it the response. That way you can fold/unfold the stack source and use the debug page as usual.

Simon.

#

ramusus (on April 25, 2009):

Is it works with dev(trunk) of django?

I copy code to utils/middleware.py and add line to settings.py:

MIDDLEWARE_CLASSES = (
    ......
    'utils.middleware.AJAXSimpleExceptionResponse'
)

but I don't see any changes in ajax exceptions output..

#

Please login first before commenting.