Extract exception handler usage to separate method.

This allow using custom exception handler on view class level,
and thus having differently configured APIs in one project.

Solves multi-configuration problem for django-rest-framework-json-api
that has a custom exception handler that must return jsonapi compatible
error response.
This commit is contained in:
Piotr Maliński 2016-04-08 16:18:38 +02:00
parent 95418eb8ac
commit 6c6d926cb3

View File

@ -425,10 +425,8 @@ class APIView(View):
else: else:
exc.status_code = status.HTTP_403_FORBIDDEN exc.status_code = status.HTTP_403_FORBIDDEN
exception_handler = self.settings.EXCEPTION_HANDLER
context = self.get_exception_handler_context() context = self.get_exception_handler_context()
response = exception_handler(exc, context) response = self._get_error_response(exc, context)
if response is None: if response is None:
raise raise
@ -436,6 +434,9 @@ class APIView(View):
response.exception = True response.exception = True
return response return response
def _get_error_response(self, exc, context):
return self.settings.EXCEPTION_HANDLER(exc, context)
# Note: Views are made CSRF exempt from within `as_view` as to prevent # Note: Views are made CSRF exempt from within `as_view` as to prevent
# accidental removal of this exemption in cases where `dispatch` needs to # accidental removal of this exemption in cases where `dispatch` needs to
# be overridden. # be overridden.