我试图在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
但是在从视图中引发异常时,这不起作用,而是抛出此消息:
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.
"""
所以我的问题是,这是一个错误吗?或者我错过了什么,还有另一种方法可以做到这一点吗?...
编辑:
这已得到rest_framework团队的正式确认。这已添加到最新版本中,因此使用v3.0.2似乎不会反映新文档 https://github.com/tomchristie/django-rest-framework/issues/2737
答案 0 :(得分:0)
我们可以使用APIException从API View引发异常,或者创建扩展APIException的自定义Exception类。
raise APIException("My Error Message")
现在,自定义异常处理程序是
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