From d1615335f95aa92092015d8dab67837f5fde30d8 Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Tue, 18 Jan 2022 16:37:57 +0100 Subject: [PATCH] Raise `ValueError` instead of `IndexError` in extract_json() (#941) * Fix exception assertion * Raise ValueError instead of IndexError in extract_json() * Make flake8 happy --- telescope/utils.py | 5 ++++- tests/test_utils.py | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/telescope/utils.py b/telescope/utils.py index 458f987f..103e780f 100644 --- a/telescope/utils.py +++ b/telescope/utils.py @@ -281,7 +281,10 @@ def extract_json(path, data): istep = int(step) except ValueError: raise ValueError(str(ke)) # Original error with step as string - data = data[istep] + try: + data = data[istep] + except IndexError: + raise ValueError(str(ke)) # Original error with step as string return data diff --git a/tests/test_utils.py b/tests/test_utils.py index 138cb15b..38c082a7 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -61,9 +61,17 @@ def test_extract_json(): assert extract_json(".pings.0", data) == 314 assert extract_json(".min_timestamp", data) == "2020-09-24T10:29:44.925" - with pytest.raises(ValueError): + with pytest.raises(ValueError) as exc_info: extract_json(".pings.a", data) + assert str(exc_info.value) == "list indices must be integers or slices, not str" + + with pytest.raises(ValueError) as exc_info: extract_json(".field", "An error returned by check") + assert str(exc_info.value) == "string indices must be integers" + + with pytest.raises(ValueError) as exc_info: + extract_json(".percentiles.75.value", {"percentiles": "No results"}) + assert str(exc_info.value) == "string indices must be integers" async def test_bugzilla_fetch_fallsback_to_empty_list(mock_aioresponses, config):