本文整理汇总了Python中rest_framework.exceptions.ErrorDetail方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.ErrorDetail方法的具体用法?Python exceptions.ErrorDetail怎么用?Python exceptions.ErrorDetail使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.exceptions
的用法示例。
在下文中一共展示了exceptions.ErrorDetail方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_token_view
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import ErrorDetail [as 别名]
def test_token_view(self):
random_address = Account.create().address
response = self.client.get(reverse('v1:token', args=(random_address,)))
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(response.data, {'detail': ErrorDetail(string='Not found.', code='not_found')})
token = TokenFactory(address=random_address)
response = self.client.get(reverse('v1:token', args=(random_address,)))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, {'address': token.address,
'logo_uri': token.get_full_logo_uri(),
'default': token.gas,
'name': token.name,
'symbol': token.symbol,
'description': token.description,
'decimals': token.decimals,
'website_uri': token.website_uri,
'gas': token.gas})
示例2: render
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import ErrorDetail [as 别名]
def render(self, data, accepted_media_type=None, renderer_context=None):
# data should be str, but in case it's a dict, return as XML.
# e.g. It happens with 404
if isinstance(data, dict):
# Force cast `ErrorDetail` as `six.text_type` because `dicttoxml`
# does not recognize this type and treat each character as xml node.
for k, v in data.items():
if isinstance(v, ErrorDetail):
data[k] = str(v)
# FIXME new `v2` list endpoint enters this block
# Submissions are wrapped in `<item>` nodes.
return dicttoxml(data, attr_type=False)
if renderer_context.get("view").action == "list":
return "<root>{}</root>".format("".join(data))
else:
return data
示例3: validate_user_password_confirm
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import ErrorDetail [as 别名]
def validate_user_password_confirm(user_data: Dict[str, Any]) -> None:
if user_data['password'] != user_data['password_confirm']:
raise ValidationError(ErrorDetail(
_("Passwords don't match"),
code='passwords-do-not-match'),
)
示例4: __init__
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import ErrorDetail [as 别名]
def __init__(self, detail=None, code=None):
if detail is None:
detail = self.default_detail
if code is None:
code = self.default_code
self.detail = ErrorDetail(detail, code)
示例5: test_update_wrong_data_sent
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import ErrorDetail [as 别名]
def test_update_wrong_data_sent(self):
data = {
"active": "wrong",
"webradio": 12,
}
response = self.client.post(self.url, data, format='json')
expected = {'active': [ErrorDetail(string='Must be a valid boolean.', code='invalid')],
'webradio': [ErrorDetail(string='Invalid pk "12" - object does not exist.', code='does_not_exist')]}
self.assertEquals(expected, response.data)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
示例6: test_update_web_radio_do_not_exist
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import ErrorDetail [as 别名]
def test_update_web_radio_do_not_exist(self):
data = {
"active": True,
"webradio": 12,
}
response = self.client.post(self.url, data, format='json')
expected = {'webradio': [ErrorDetail(string='Invalid pk "12" - object does not exist.', code='does_not_exist')]}
self.assertEquals(expected, response.data)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
示例7: test_set_volume_invalid
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import ErrorDetail [as 别名]
def test_set_volume_invalid(self):
data = {
"volume": 200
}
response = self.client.post(self.url, data, format='json')
expected = {'volume':
[ErrorDetail(string='Ensure this value is less than or equal to 100.',
code='max_value')]}
self.assertEquals(expected, response.data)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
示例8: errors
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import ErrorDetail [as 别名]
def errors(self):
ret = super(Serializer, self).errors
if isinstance(ret, list) and len(ret) == 1 and getattr(ret[0], 'code', None) == 'null':
# Edge case. Provide a more descriptive error than
# "this field may not be null", when no data is passed.
detail = ErrorDetail('No data provided', code='null')
ret = {api_settings.NON_FIELD_ERRORS_KEY: [detail]}
return ReturnDict(ret, serializer=self)
# There's some replication of `ListField` here,
# but that's probably better than obfuscating the call hierarchy.
示例9: get_error_detail
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import ErrorDetail [as 别名]
def get_error_detail(exc_info):
"""
Given a Django ValidationError, return a list of ErrorDetail,
with the `code` populated.
"""
code = getattr(exc_info, 'code', None) or 'invalid'
return [
ErrorDetail(msg, code=code)
for msg in exc_info.messages
]
示例10: __init__
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import ErrorDetail [as 别名]
def __init__(self, detail=None, code=None, reinit=True):
message = detail if detail is not None else self.default_detail
self.code = code if code is not None else self.default_code
self.detail = {
'detail': exceptions.ErrorDetail(message, self.default_code),
'reinit': reinit,
}
示例11: _get_list_of_errors
# 需要导入模块: from rest_framework import exceptions [as 别名]
# 或者: from rest_framework.exceptions import ErrorDetail [as 别名]
def _get_list_of_errors(self, field_path='', errors_dict=None):
"""
Error_dict is in the following format:
{
'field1': {
'message': 'some message..'
'code' 'some code...'
},
'field2: ...'
}
"""
if errors_dict is None:
return []
message_value = errors_dict.get(self.MESSAGE, None)
# Note: If 'message' is name of a field we don't want to stop the recursion here!
if message_value is not None and\
(type(message_value) in {str, exceptions.ErrorDetail}):
if field_path:
errors_dict[self.FIELD] = field_path
return [errors_dict]
errors_list = []
for key, value in errors_dict.items():
new_field_path = '{0}.{1}'.format(field_path, key) if field_path else key
key_is_non_field_errors = key == api_settings.NON_FIELD_ERRORS_KEY
if type(value) is list:
current_level_error_list = []
new_value = value
for error in new_value:
# if the type of field_error is list we need to unpack it
field_error = self._unpack(error)
if not key_is_non_field_errors:
field_error[self.FIELD] = new_field_path
current_level_error_list.append(field_error)
else:
path = field_path if key_is_non_field_errors else new_field_path
current_level_error_list = self._get_list_of_errors(field_path=path, errors_dict=value)
errors_list += current_level_error_list
return errors_list