Skip to content

Commit

Permalink
Raise ValueError instead of IndexError in extract_json() (#941)
Browse files Browse the repository at this point in the history
* Fix exception assertion

* Raise ValueError instead of IndexError in extract_json()

* Make flake8 happy
  • Loading branch information
leplatrem authored Jan 18, 2022
1 parent ef6ec54 commit d161533
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 4 additions & 1 deletion telescope/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
10 changes: 9 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit d161533

Please sign in to comment.