Ticket #5889: handler500_example.patch

File handler500_example.patch, 1.4 KB (added by Mikko Ohtamaa <mikko@…>, 8 years ago)

Patch against Django trunk root

  • home/moo/workspace/django-trunk/docs/request_response.txt

     
    588588That takes care of setting ``handler500`` in the current module. As you can see
    589589in ``django/conf/urls/defaults.py``, ``handler500`` is set to
    590590``'django.views.defaults.server_error'`` by default.
     591
     592Here is example how to log the exception using Python logging API and
     593display an error page on the production server::
     594
     595        from django.http import HttpResponseServerError
     596    import sys
     597
     598        def handle_exception(request, *args, **kwargs):
     599            """ Custom exception handler for Django.
     600           
     601            Note that settings.DEBUG must be False or
     602            this handler is never run.
     603            """
     604           
     605            # Get the latest exception from Python system service
     606            exception = sys.exc_info()[0]
     607       
     608            # Use  Python logging module to log the exception
     609            # For more information see:
     610            # http://docs.python.org/lib/module-logging.html
     611            logger.error("Uncaught exception got through, rendering 500 page")
     612            logger.exception(exception)
     613           
     614            # Output user visible HTTP response
     615            return HttpResponseServerError(render_to_string("pages/500.html"))
     616 No newline at end of file
Back to Top