| 12345678910111213141516171819202122232425 |
- """
- 自定义异常处理
- 通过restformwork 的setting配置引用 REST_FRAMEWORK --> EXCEPTION_HANDLER
- """
- from rest_framework.views import exception_handler
- from rest_framework.views import Response
- from rest_framework import status
- def custom_exception_handler(exc, context):
- response = exception_handler(exc, context)
- if response is None:
- return Response({
- 'code': status.HTTP_500_INTERNAL_SERVER_ERROR,
- 'msg': 'error:{exc}'.format(exc=exc),
- 'data': ''
- }, status=status.HTTP_500_INTERNAL_SERVER_ERROR, exception=True)
- else:
- return Response({
- 'code': response.status_code,
- 'msg': 'error:{exc}'.format(exc=exc),
- 'data': ''
- }, status=200, exception=True)
|