【发布时间】:2014-05-11 01:47:54
【问题描述】:
我正在尝试从 REST Django 框架创建自定义错误响应。
我在 views.py 中包含以下内容,
from rest_framework.views import exception_handler
def custom_exception_handler(exc):
"""
Custom exception handler for Django Rest Framework that adds
the `status_code` to the response and renames the `detail` key to `error`.
"""
response = exception_handler(exc)
if response is not None:
response.data['status_code'] = response.status_code
response.data['error'] = response.data['detail']
response.data['detail'] = "CUSTOM ERROR"
return response
并且还在 settings.py 中添加了以下内容。
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
),
'EXCEPTION_HANDLER': 'project.input.utils.custom_exception_handler'
}
我是否错过了什么,因为我没有得到预期的响应。即 400 API 响应中的自定义错误消息。
谢谢,
【问题讨论】: