Custom exception handler not working as documented

2019-05-17 07:47发布

问题:

I'm trying to write a custom exception handler in django-rest-framework, and the code is the same as given in the example:

from rest_framework.views import exception_handler

def custom_exception_handler(exc, context):
    # Call REST framework's default exception handler first,
    # to get the standard error response.
    response = exception_handler(exc, context)

    # Now add the HTTP status code to the response.
    if response is not None:
        response.data['status_code'] = response.status_code

    return response

But on raising an exception from the view, this does not work, it instead throws this message:

custom_exception_handler() missing 1 required positional argument: 'context'

I've tried setting the first argument to None, like so:

def custom_exception_handler(exc, context=None):

But this happens:

exception_handler() takes 1 positional argument but 2 were given

So it seems rest_framework.views.exception_handler takes only one argument.
Indeed this is the case:

def exception_handler(exc):
    """
    Returns the response that should be used for any given exception.

    By default we handle the REST framework `APIException`, and also
    Django's built-in `ValidationError`, `Http404` and `PermissionDenied`
    exceptions.

    Any unhandled exceptions may return `None`, which will cause a 500 error
    to be raised.
    """

So my question is, is this a bug? Or am i missing something, and there is another way to do this?..


EDIT:

This has been confirmed officially by the rest_framework team. This has been added in the latest version so it seems using v3.0.2 will not reflect the new documentation.
https://github.com/tomchristie/django-rest-framework/issues/2737

回答1:

We can raise exception from API View using APIException or create custom Exception class that extends APIException.

raise APIException("My Error Message")

Now Custom Exception handler is

def custom_exception_handler(exc, context):
    # Call REST framework's default exception handler first,
    # to get the standard error response.
    response = exception_handler(exc, context)
    if isinstance(exc, APIException):
        response.data = {}
        response.data['error'] = str(exc)
    elif isinstance(exc, MyCustomException):
        response.data = {}
        response.data['error'] = str(exc)
    return response