DRF:自定义来自API的异常消息



最近我开始深入研究DRF,我想知道我是否要开始定制通过API返回的错误消息以获得不正确的权限,我想补充一点细节。

例如,如果没有为权限受限的端点提供身份验证凭据,API将返回:

{
"detail": "Authentication credentials were not provided."
}

它来自rest_framework.exceptions的第171行:https://github.com/encode/django-rest-framework/blob/master/rest_framework/exceptions.py.真的,我希望这与一致

{
"success": false,
"message": "Authentication credentials were not provided.",
"data": null
}

所以,我想我现在需要开始定制我自己的例外。

我应该如何最好地做这件事?

也许它与序列化程序中的default_error_messages = {}有某种联系。。。

您可以在settings.py:上覆盖DRF的默认异常处理程序和JSON解析器

REST_FRAMEWORK = {
...
'EXCEPTION_HANDLER': 'helpers.exceptions.custom_exception_handler',
'DEFAULT_RENDERER_CLASSES': [
'helpers.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
]
}

然后,这只是一个定制如何处理异常以及如何呈现响应的问题:

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)
# Customize your exception handling here
return response

如果你需要对响应进行任何额外的格式化,你可以使用自定义的JSON呈现器,在我的情况下,我不得不向负载中添加一个"status_code":

class JSONRenderer(BaseJsonRenderer):
def render(self, data, accepted_media_type=None, renderer_context=None):
"""
Render `data` into JSON, returning a bytestring.
"""
<Base code from the original class...>
response = renderer_context.get('response')
if response and 200 <= response.status_code <= 299 and 'status_code' not in response.data:
response.data = Errors.success(response.data)
<Base code from the original class...>

我的Errors.success(response.data)只是将成功状态代码合并到数据中的一种更简单的方法。

有一个装饰器解决方案可以在每种类型的异常上创建自定义响应:

# project/api/exceptions.py
from functools import wraps
from rest_framework import status

def handle_exceptions(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except AuthCredentialsError as exc:
return Response(
{"message": exc.message},
status=status.HTTP_405_METHOD_NOT_ALLOWED,
)
return wrapper

# project/api/your_code.py
from project.api.exceptions import handle_exceptions

class SomeViewSet():
@handle_exceptions
def create(self, request, *args, **kwargs):
raise AuthCredentialsError("Authentication credentials were not provided")

最新更新