自定义异常处理程序无法按照 django-rest-framework 中记录的方式工作

kun*_*unl 5 python django django-rest-framework

我正在尝试在 django-rest-framework 中编写自定义异常处理程序,代码与示例中给出的相同:

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
Run Code Online (Sandbox Code Playgroud)

但是在从视图中引发异常时,这不起作用,而是抛出以下消息:

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

我尝试将第一个参数设置为None,如下所示:

def custom_exception_handler(exc, context=None):

但这种情况发生了:

exception_handler() takes 1 positional argument but 2 were given

所以看起来rest_framework.views.exception_handler只需要一个参数。
确实是这样的:

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.
    """
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,这是一个错误吗?或者我错过了什么,还有另一种方法可以做到这一点?


编辑:

这已经得到rest_framework团队的正式确认。这已在最新版本中添加,因此使用 v3.0.2 似乎不会反映新文档。
https://github.com/tomchristie/django-rest-framework/issues/2737

小智 1

我们可以使用 APIException 从 API 视图引发异常,或者创建扩展 APIException 的自定义异常类。

raise APIException("My Error Message")
Run Code Online (Sandbox Code Playgroud)

现在自定义异常处理程序是

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
Run Code Online (Sandbox Code Playgroud)