preserve json parsing error (#58461)
* preserve json parsing error * added test
This commit is contained in:
parent
23ab8f37df
commit
bbdf77a59f
7 changed files with 29 additions and 12 deletions
|
@ -75,11 +75,11 @@ class DataLoader:
|
|||
def set_vault_secrets(self, vault_secrets):
|
||||
self._vault.secrets = vault_secrets
|
||||
|
||||
def load(self, data, file_name='<string>', show_content=True):
|
||||
def load(self, data, file_name='<string>', show_content=True, json_only=False):
|
||||
'''Backwards compat for now'''
|
||||
return from_yaml(data, file_name, show_content, self._vault.secrets)
|
||||
return from_yaml(data, file_name, show_content, self._vault.secrets, json_only=json_only)
|
||||
|
||||
def load_from_file(self, file_name, cache=True, unsafe=False):
|
||||
def load_from_file(self, file_name, cache=True, unsafe=False, json_only=False):
|
||||
''' Loads data from a file, which can contain either JSON or YAML. '''
|
||||
|
||||
file_name = self.path_dwim(file_name)
|
||||
|
@ -94,7 +94,7 @@ class DataLoader:
|
|||
(b_file_data, show_content) = self._get_file_contents(file_name)
|
||||
|
||||
file_data = to_text(b_file_data, errors='surrogate_or_strict')
|
||||
parsed_data = self.load(data=file_data, file_name=file_name, show_content=show_content)
|
||||
parsed_data = self.load(data=file_data, file_name=file_name, show_content=show_content, json_only=json_only)
|
||||
|
||||
# cache the file contents for next time
|
||||
self._FILE_CACHE[file_name] = parsed_data
|
||||
|
|
|
@ -13,7 +13,7 @@ from yaml import YAMLError
|
|||
|
||||
from ansible.errors import AnsibleParserError
|
||||
from ansible.errors.yaml_strings import YAML_SYNTAX_ERROR
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.module_utils._text import to_native, to_text
|
||||
from ansible.parsing.yaml.loader import AnsibleLoader
|
||||
from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject
|
||||
from ansible.parsing.ajson import AnsibleJSONDecoder
|
||||
|
@ -22,7 +22,7 @@ from ansible.parsing.ajson import AnsibleJSONDecoder
|
|||
__all__ = ('from_yaml',)
|
||||
|
||||
|
||||
def _handle_error(yaml_exc, file_name, show_content):
|
||||
def _handle_error(json_exc, yaml_exc, file_name, show_content):
|
||||
'''
|
||||
Optionally constructs an object (AnsibleBaseYAMLObject) to encapsulate the
|
||||
file name/position where a YAML exception occurred, and raises an AnsibleParserError
|
||||
|
@ -36,9 +36,10 @@ def _handle_error(yaml_exc, file_name, show_content):
|
|||
err_obj = AnsibleBaseYAMLObject()
|
||||
err_obj.ansible_pos = (file_name, yaml_exc.problem_mark.line + 1, yaml_exc.problem_mark.column + 1)
|
||||
|
||||
err_msg = getattr(yaml_exc, 'problem', '')
|
||||
err_msg = 'We were unable to read either as JSON nor YAML, these are the errors we got from each:\n' \
|
||||
'JSON: %s\n\n' % to_text(json_exc) + YAML_SYNTAX_ERROR % getattr(yaml_exc, 'problem', '')
|
||||
|
||||
raise AnsibleParserError(YAML_SYNTAX_ERROR % to_native(err_msg), obj=err_obj, show_content=show_content, orig_exc=yaml_exc)
|
||||
raise AnsibleParserError(to_native(err_msg), obj=err_obj, show_content=show_content, orig_exc=yaml_exc)
|
||||
|
||||
|
||||
def _safe_load(stream, file_name=None, vault_secrets=None):
|
||||
|
@ -54,7 +55,7 @@ def _safe_load(stream, file_name=None, vault_secrets=None):
|
|||
pass # older versions of yaml don't have dispose function, ignore
|
||||
|
||||
|
||||
def from_yaml(data, file_name='<string>', show_content=True, vault_secrets=None):
|
||||
def from_yaml(data, file_name='<string>', show_content=True, vault_secrets=None, json_only=False):
|
||||
'''
|
||||
Creates a python datastructure from the given data, which can be either
|
||||
a JSON or YAML string.
|
||||
|
@ -68,11 +69,15 @@ def from_yaml(data, file_name='<string>', show_content=True, vault_secrets=None)
|
|||
# we first try to load this data as JSON.
|
||||
# Fixes issues with extra vars json strings not being parsed correctly by the yaml parser
|
||||
new_data = json.loads(data, cls=AnsibleJSONDecoder)
|
||||
except Exception:
|
||||
except Exception as json_exc:
|
||||
|
||||
if json_only:
|
||||
raise AnsibleParserError(to_native(json_exc), orig_exc=json_exc)
|
||||
|
||||
# must not be JSON, let the rest try
|
||||
try:
|
||||
new_data = _safe_load(data, file_name=file_name, vault_secrets=vault_secrets)
|
||||
except YAMLError as yaml_exc:
|
||||
_handle_error(yaml_exc, file_name, show_content)
|
||||
_handle_error(json_exc, yaml_exc, file_name, show_content)
|
||||
|
||||
return new_data
|
||||
|
|
|
@ -128,7 +128,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable):
|
|||
raise AnsibleError("Inventory {0} contained characters that cannot be interpreted as UTF-8: {1}".format(path, to_native(e)))
|
||||
|
||||
try:
|
||||
processed = self.loader.load(data)
|
||||
processed = self.loader.load(data, json_only=True)
|
||||
except Exception as e:
|
||||
raise AnsibleError("failed to parse executable inventory script results from {0}: {1}\n{2}".format(path, to_native(e), err))
|
||||
|
||||
|
|
1
test/integration/targets/dataloader/aliases
Normal file
1
test/integration/targets/dataloader/aliases
Normal file
|
@ -0,0 +1 @@
|
|||
shippable/posix/group1
|
|
@ -0,0 +1,4 @@
|
|||
- hosts: localhost
|
||||
gather_facts: false
|
||||
vars_files:
|
||||
- vars/invalid.json
|
6
test/integration/targets/dataloader/runme.sh
Executable file
6
test/integration/targets/dataloader/runme.sh
Executable file
|
@ -0,0 +1,6 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -eux
|
||||
|
||||
# check if we get proper json error
|
||||
ansible-playbook -i ../../inventory attempt_to_load_invalid_json.yml "$@" 2>&1|grep 'JSON:'
|
1
test/integration/targets/dataloader/vars/invalid.json
Normal file
1
test/integration/targets/dataloader/vars/invalid.json
Normal file
|
@ -0,0 +1 @@
|
|||
{ }}
|
Loading…
Reference in a new issue