settings.py
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'xxx.custom_exception_handler',
}
新建.py文件,名称任意
xxx.py
from django.db import DatabaseError
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import exception_handler as drf_exception_handler
def exception_handler(exc, context):
response = drf_exception_handler(exc, context)
if response is None:
view = context['view'] # 出错的方法或者函数名称
if isinstance(exc, DatabaseError):
print('[%s]: %s' % (view, exc))
response = Response({'detail': '服务器内部错误'}, status=status.HTTP_507_INSUFFICIENT_STORAGE)
return response
标签:exception,exc,自定义,rest,django,handler,import,补获,response From: https://www.cnblogs.com/vPYer/p/17602918.html