From e77c71ede39c68984f72321396ae9e5dc43706ea Mon Sep 17 00:00:00 2001 From: jun0jang Date: Thu, 28 Nov 2019 12:56:05 +0900 Subject: [PATCH] Revert Change excpetion_handler This reverts commit 13c379c18935e16411b3cdfe701fe9be93c16333. --- rest_framework/exceptions.py | 12 ++++++++---- rest_framework/views.py | 4 ++-- tests/test_permissions.py | 8 -------- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index 4e846c39e..345a40524 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -101,8 +101,10 @@ class APIException(Exception): default_code = 'error' def __init__(self, detail=None, code=None): - detail = detail or self.default_detail - code = code or self.default_code + if detail is None: + detail = self.default_detail + if code is None: + code = self.default_code self.detail = _get_error_details(detail, code) @@ -139,8 +141,10 @@ class ValidationError(APIException): default_code = 'invalid' def __init__(self, detail=None, code=None): - detail = detail or self.default_detail - code = code or self.default_code + if detail is None: + detail = self.default_detail + if code is None: + code = self.default_code # For validation failures, we may collect many errors together, # so the details should always be coerced to a list if not already. diff --git a/rest_framework/views.py b/rest_framework/views.py index f2b454093..7c21edf5a 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -90,10 +90,10 @@ def exception_handler(exc, context): if getattr(exc, 'wait', None): headers['Retry-After'] = '%d' % exc.wait - if isinstance(exc.detail, (list, tuple, dict)): + if isinstance(exc.detail, (list, dict)): data = exc.detail else: - data = {'detail': exc.detail, 'code': exc.detail.code} + data = {'detail': exc.detail} set_rollback() return Response(data, status=exc.status_code, headers=headers) diff --git a/tests/test_permissions.py b/tests/test_permissions.py index 71dbf0a37..8fbee25dc 100644 --- a/tests/test_permissions.py +++ b/tests/test_permissions.py @@ -499,34 +499,26 @@ class CustomPermissionsTests(TestCase): def test_permission_denied(self): response = denied_view(self.request, pk=1) detail = response.data.get('detail') - code = response.data.get('code') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertNotEqual(detail, self.custom_message) - self.assertNotEqual(code, self.custom_code) def test_permission_denied_with_custom_detail(self): response = denied_view_with_detail(self.request, pk=1) detail = response.data.get('detail') - code = response.data.get('code') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(detail, self.custom_message) - self.assertEqual(code, self.custom_code) def test_permission_denied_for_object(self): response = denied_object_view(self.request, pk=1) detail = response.data.get('detail') - code = response.data.get('code') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertNotEqual(detail, self.custom_message) - self.assertNotEqual(code, self.custom_code) def test_permission_denied_for_object_with_custom_detail(self): response = denied_object_view_with_detail(self.request, pk=1) detail = response.data.get('detail') - code = response.data.get('code') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(detail, self.custom_message) - self.assertEqual(code, self.custom_code) class PermissionsCompositionTests(TestCase):