Skip to content

Commit

Permalink
Merge pull request #37 from elupus/json_exception
Browse files Browse the repository at this point in the history
Catch json exception and convert to known type
  • Loading branch information
elupus authored May 21, 2024
2 parents b76edb1 + 2e92ab7 commit 8d9524f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion haphilipsjs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ def decode_xtv_json(text: str):
text = text.replace(",}", "}")
while (p := text.find(",,")) >= 0:
text = text[:p] + text[p + 1 :]
data = json.loads(text)

try:
data = json.loads(text)
except json.decoder.JSONDecodeError as exception:
raise NoneJsonData(text) from exception

return data

Expand Down
2 changes: 2 additions & 0 deletions tests/test_v6.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@ async def test_buggy_json():
assert haphilipsjs.decode_xtv_json('{,"a":{}}') == {"a": {}}
assert haphilipsjs.decode_xtv_json('{"a":{},}') == {"a": {}}
assert haphilipsjs.decode_xtv_json('{"a":{},,,"b":{}}') == {"a": {}, "b": {}}
with pytest.raises(haphilipsjs.NoneJsonData):
haphilipsjs.decode_xtv_json("Plain text data")

async def test_get_recordings(client_mock):
"""Verify that we can read back selected recording values"""
Expand Down

0 comments on commit 8d9524f

Please sign in to comment.