When you first start working with django one of the best parts is the incredibly detailed debug page that shows up whenever you inevitably do something wrong.
Of course, your users aren't going to appreciate seeing that page, so on a real installation of your system you turn off DEBUG and then you'll never see that beautiful page again.
Unless of course you generate it and save it out on every error with middleware.
We had a piece of debug saving middleware with a process_exception method that roughly looked like:
from django.views import debug
def process_exception(self, request, exception):
if exception.__class__ is http.Http404:
response = debug.technical_404_response(
request, exception)
else:
response = debug.technical_500_response(
request, *sys.exc_info())
save_page(response._get_content())
return None
Where save_page took care of putting it in the proper directory for the installation, doing a rotation so only the last N debug pages would be saved, naming, and so on.
The return value is None because we don't actually want to interfere with regular exception processing (in this case a normal 404 or 500 page) we just want to write the exception down as it goes by. Returning None keeps things rolling.
See the django docs on middleware for more about that and on how to install middleware and all your other "what is middleware" needs.
No comments:
Post a Comment