Use consistent error message when vars are not parsed correctly

This commit is contained in:
AlanCoding 2017-03-22 14:26:12 -04:00
parent b5ae83845e
commit 99fc0e8264
2 changed files with 5 additions and 4 deletions

View File

@ -1216,7 +1216,7 @@ class HostSerializer(BaseSerializerWithVariables):
vars_dict['ansible_ssh_port'] = port
attrs['variables'] = yaml.dump(vars_dict)
except (yaml.YAMLError, TypeError):
raise serializers.ValidationError(_('Must be valid JSON or YAML.'))
raise serializers.ValidationError({'variables': _('Must be valid JSON or YAML.')})
return super(HostSerializer, self).validate(attrs)
@ -2693,7 +2693,7 @@ class JobLaunchSerializer(BaseSerializer):
extra_vars = yaml.safe_load(extra_vars)
assert isinstance(extra_vars, dict)
except (yaml.YAMLError, TypeError, AttributeError, AssertionError):
errors['extra_vars'] = _('Must be a valid JSON or YAML dictionary.')
errors['extra_vars'] = _('Must be valid JSON or YAML.')
if not isinstance(extra_vars, dict):
extra_vars = {}
@ -2764,7 +2764,7 @@ class WorkflowJobLaunchSerializer(BaseSerializer):
extra_vars = yaml.safe_load(extra_vars)
assert isinstance(extra_vars, dict)
except (yaml.YAMLError, TypeError, AttributeError, AssertionError):
errors['extra_vars'] = 'Must be a valid JSON or YAML dictionary.'
errors['extra_vars'] = _('Must be valid JSON or YAML.')
if not isinstance(extra_vars, dict):
extra_vars = {}

View File

@ -196,7 +196,8 @@ def test_job_reject_invalid_prompted_extra_vars(runtime_data, job_template_promp
reverse('api:job_template_launch', args=[job_template.pk]),
dict(extra_vars='{"unbalanced brackets":'), admin_user, expect=400)
assert response.data['extra_vars'] == ['Must be a valid JSON or YAML dictionary.']
assert 'extra_vars' in response.data
assert 'valid JSON or YAML' in str(response.data['extra_vars'][0])
@pytest.mark.django_db