mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-07 13:54:47 +03:00
Added 'get_codes' and 'get_full_details'
This commit is contained in:
parent
2eb62d4b23
commit
ce8eb7ecce
|
@ -20,7 +20,7 @@ from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList
|
||||||
def _get_error_details(data, default_code=None):
|
def _get_error_details(data, default_code=None):
|
||||||
"""
|
"""
|
||||||
Descend into a nested data structure, forcing any
|
Descend into a nested data structure, forcing any
|
||||||
lazy translation strings or strings into `ErrorMessage`.
|
lazy translation strings or strings into `ErrorDetail`.
|
||||||
"""
|
"""
|
||||||
if isinstance(data, list):
|
if isinstance(data, list):
|
||||||
ret = [
|
ret = [
|
||||||
|
@ -43,6 +43,25 @@ def _get_error_details(data, default_code=None):
|
||||||
return ErrorDetail(text, code)
|
return ErrorDetail(text, code)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_codes(detail):
|
||||||
|
if isinstance(detail, list):
|
||||||
|
return [_get_codes(item) for item in detail]
|
||||||
|
elif isinstance(detail, dict):
|
||||||
|
return {key: _get_codes(value) for key, value in detail.items()}
|
||||||
|
return detail.code
|
||||||
|
|
||||||
|
|
||||||
|
def _get_full_details(detail):
|
||||||
|
if isinstance(detail, list):
|
||||||
|
return [_get_full_details(item) for item in detail]
|
||||||
|
elif isinstance(detail, dict):
|
||||||
|
return {key: _get_full_details(value) for key, value in detail.items()}
|
||||||
|
return {
|
||||||
|
'message': detail,
|
||||||
|
'code': detail.code
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class ErrorDetail(six.text_type):
|
class ErrorDetail(six.text_type):
|
||||||
"""
|
"""
|
||||||
A string-like object that can additionally
|
A string-like object that can additionally
|
||||||
|
@ -99,6 +118,12 @@ class ValidationError(APIException):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return six.text_type(self.detail)
|
return six.text_type(self.detail)
|
||||||
|
|
||||||
|
def get_codes(self):
|
||||||
|
return _get_codes(self.detail)
|
||||||
|
|
||||||
|
def get_full_details(self):
|
||||||
|
return _get_full_details(self.detail)
|
||||||
|
|
||||||
|
|
||||||
class ParseError(APIException):
|
class ParseError(APIException):
|
||||||
status_code = status.HTTP_400_BAD_REQUEST
|
status_code = status.HTTP_400_BAD_REQUEST
|
||||||
|
|
|
@ -30,16 +30,8 @@ class TestValidationErrorWithCode(TestCase):
|
||||||
self.DEFAULT_HANDLER = api_settings.EXCEPTION_HANDLER
|
self.DEFAULT_HANDLER = api_settings.EXCEPTION_HANDLER
|
||||||
|
|
||||||
def exception_handler(exc, request):
|
def exception_handler(exc, request):
|
||||||
return_errors = {}
|
data = exc.get_full_details()
|
||||||
for field_name, errors in exc.detail.items():
|
return Response(data, status=status.HTTP_400_BAD_REQUEST)
|
||||||
return_errors[field_name] = []
|
|
||||||
for error in errors:
|
|
||||||
return_errors[field_name].append({
|
|
||||||
'code': error.code,
|
|
||||||
'message': error
|
|
||||||
})
|
|
||||||
|
|
||||||
return Response(return_errors, status=status.HTTP_400_BAD_REQUEST)
|
|
||||||
|
|
||||||
api_settings.EXCEPTION_HANDLER = exception_handler
|
api_settings.EXCEPTION_HANDLER = exception_handler
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user