diff --git a/netcompare/check_types.py b/netcompare/check_types.py index 10ea841..55d194b 100644 --- a/netcompare/check_types.py +++ b/netcompare/check_types.py @@ -72,6 +72,9 @@ def get_value(output: Union[Mapping, List], path: str = "*", exclude: List = Non values = jmespath.search(jmespath_value_parser(path), output) + if values is None: + raise TypeError("JMSPath returned 'None'. Please, verify your JMSPath regex.") + # check for multi-nested lists if not found return here if not any(isinstance(i, list) for i in values): return values diff --git a/tests/test_get_value.py b/tests/test_get_value.py new file mode 100644 index 0000000..13a2974 --- /dev/null +++ b/tests/test_get_value.py @@ -0,0 +1,17 @@ +"""Test GitHub issues.""" +import pytest +from netcompare import CheckType + + +my_data = [{"global": {"peers": {"10.1.0.0": "peer1", "10.2.0.0": "peer2"}}}] + + +@pytest.mark.parametrize("data", my_data) +def test_jmspath_return_none(data): + """Handle exception when JMSPath retunr None.""" + my_jmspath = "global[*]" + my_check = CheckType.init(check_type="exact_match") + with pytest.raises(TypeError) as error: + my_check.get_value(output=data, path=my_jmspath)() # pylint: disable=E0110 + + assert "JMSPath returned 'None'. Please, verify your JMSPath regex." in error.value.__str__()