From 3f07ec3d73f59b9fdd77757612e93d8bed711f24 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Thu, 6 Mar 2014 22:07:35 -0500 Subject: [PATCH] Fixup JSON error reporting in previous commits. --- lib/ansible/utils/__init__.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index 1d09cfde990..dd2989a94b5 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -351,13 +351,19 @@ def smush_ds(data): else: 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!!!''' data = data.lstrip() if data.startswith("{") or data.startswith("["): # since the line starts with { or [ we can infer this is a JSON document. - loaded = json.loads(data) + try: + 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 this is pretty sure to be a YAML document loaded = yaml.safe_load(data) @@ -522,7 +528,7 @@ def parse_yaml_from_file(path, vault_password=None): data = vault.decrypt(data) try: - return parse_yaml(data) + return parse_yaml(data, path_hint=path) except yaml.YAMLError, exc: process_yaml_error(exc, data, path)