def custom_exception_handler(exc, context):
"""
Custom django rest framework exception handler that includes 'status_code' field in the
responses.
"""
# Call REST framework's default exception handler first
response = exception_handler(exc, context)
# Catch unexpected exceptions
if response is None:
if isinstance(exc, Exception):
data = {'detail': 'A server error occurred.'}
set_rollback()
response = Response(data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# Now add the HTTP status code to the response.
if response is not None:
response.data['status_code'] = response.status_code
return response
评论列表
文章目录