mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-28 00:49:49 +03:00
parent
d1ebf825c6
commit
e77c71ede3
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user