From fa4ce50be7dc0d429d6455e02a5da67a7d0fcb92 Mon Sep 17 00:00:00 2001 From: Akhil Lawrence Date: Wed, 10 Aug 2016 19:54:32 +0530 Subject: [PATCH] Modified exception handler to throw django error page in case of 500 error (#4172) Show Traceback HTML in browsable API --- rest_framework/views.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/rest_framework/views.py b/rest_framework/views.py index 41d108e53..b86bb7eaa 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -3,13 +3,17 @@ Provides an APIView class that is the base of all views in REST framework. """ from __future__ import unicode_literals +import sys + +from django.conf import settings from django.core.exceptions import PermissionDenied from django.db import models from django.http import Http404 -from django.http.response import HttpResponseBase +from django.http.response import HttpResponse, HttpResponseBase from django.utils import six from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ +from django.views import debug from django.views.decorators.csrf import csrf_exempt from django.views.generic import View @@ -91,7 +95,11 @@ def exception_handler(exc, context): set_rollback() return Response(data, status=status.HTTP_403_FORBIDDEN) - # Note: Unhandled exceptions will raise a 500 error. + # throw django's error page if debug is True + if settings.DEBUG: + exception_reporter = debug.ExceptionReporter(context.get('request'), *sys.exc_info()) + return HttpResponse(exception_reporter.get_traceback_html(), status=500) + return None