1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Fixup JSON error reporting in previous commits.

This commit is contained in:
Michael DeHaan 2014-03-06 22:07:35 -05:00
parent a4d01b0891
commit 3f07ec3d73

View file

@ -351,13 +351,19 @@ def smush_ds(data):
else: else:
return data return data
def parse_yaml(data): def parse_yaml(data, path_hint=None):
''' convert a yaml string to a data structure. Also supports JSON, ssssssh!!!''' ''' convert a yaml string to a data structure. Also supports JSON, ssssssh!!!'''
data = data.lstrip() data = data.lstrip()
if data.startswith("{") or data.startswith("["): if data.startswith("{") or data.startswith("["):
# since the line starts with { or [ we can infer this is a JSON document. # since the line starts with { or [ we can infer this is a JSON document.
try:
loaded = json.loads(data) loaded = json.loads(data)
except ValueError, ve:
if path_hint:
raise errors.AnsibleError(path_hint + ": " + str(ve))
else:
raise errors.AnsibleError(str(ve))
else: else:
# else this is pretty sure to be a YAML document # else this is pretty sure to be a YAML document
loaded = yaml.safe_load(data) loaded = yaml.safe_load(data)
@ -522,7 +528,7 @@ def parse_yaml_from_file(path, vault_password=None):
data = vault.decrypt(data) data = vault.decrypt(data)
try: try:
return parse_yaml(data) return parse_yaml(data, path_hint=path)
except yaml.YAMLError, exc: except yaml.YAMLError, exc:
process_yaml_error(exc, data, path) process_yaml_error(exc, data, path)