Example #1
0
    def handle_exception(self, exc, request):
        from django.utils.log import getLogger
        from django.conf import settings

        logger = getLogger('django.request')
        exc_info = sys.exc_info()

        logger.error(
            'Internal Server Error: %s', request.path,
            exc_info=exc_info,
            extra={
                'status_code': 500,
                'request': request
            }
        )

        resp = HttpResponse('', status=500, content_type='text/plain')
        resp.content = ''

        if hasattr(exc, 'resp_obj'):
            resp = exc.resp_obj
            resp.status_code = 500
            resp.set_common_headers(request)
        else:
            resp = HttpResponse('', status=500, content_type='text/plain')

        resp.content = 'An error occured.'

        if settings.DEBUG:
            from django.views.debug import ExceptionReporter
            reporter = ExceptionReporter(request, *exc_info)
            resp.content = reporter.get_traceback_text()

        resp['Content-Type'] = 'text/plain'
        return resp