Include filename in exception when loading a json file fails (#111802)

* Include filename in exception when loading a json file fails

* fix
This commit is contained in:
J. Nick Koston 2024-02-29 05:30:29 -10:00 committed by GitHub
parent 9512fb420d
commit f59268b2ee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 11 additions and 10 deletions

View file

@ -79,12 +79,12 @@ def load_json(
except FileNotFoundError: except FileNotFoundError:
# This is not a fatal error # This is not a fatal error
_LOGGER.debug("JSON file not found: %s", filename) _LOGGER.debug("JSON file not found: %s", filename)
except ValueError as error: except JSON_DECODE_EXCEPTIONS as error:
_LOGGER.exception("Could not parse JSON content: %s", filename) _LOGGER.exception("Could not parse JSON content: %s", filename)
raise HomeAssistantError(error) from error raise HomeAssistantError(f"Error while loading {filename}: {error}") from error
except OSError as error: except OSError as error:
_LOGGER.exception("JSON file reading failed: %s", filename) _LOGGER.exception("JSON file reading failed: %s", filename)
raise HomeAssistantError(error) from error raise HomeAssistantError(f"Error while loading {filename}: {error}") from error
return {} if default is _SENTINEL else default return {} if default is _SENTINEL else default

View file

@ -706,8 +706,8 @@ async def test_loading_corrupt_core_file(
assert issue_entry.translation_placeholders["storage_key"] == storage_key assert issue_entry.translation_placeholders["storage_key"] == storage_key
assert issue_entry.issue_domain == HOMEASSISTANT_DOMAIN assert issue_entry.issue_domain == HOMEASSISTANT_DOMAIN
assert ( assert (
issue_entry.translation_placeholders["error"] "unexpected character: line 1 column 1 (char 0)"
== "unexpected character: line 1 column 1 (char 0)" in issue_entry.translation_placeholders["error"]
) )
files = await hass.async_add_executor_job( files = await hass.async_add_executor_job(
@ -767,8 +767,8 @@ async def test_loading_corrupt_file_known_domain(
assert issue_entry.translation_placeholders["storage_key"] == storage_key assert issue_entry.translation_placeholders["storage_key"] == storage_key
assert issue_entry.issue_domain == "testdomain" assert issue_entry.issue_domain == "testdomain"
assert ( assert (
issue_entry.translation_placeholders["error"] "unexpected content after document: line 1 column 17 (char 16)"
== "unexpected content after document: line 1 column 17 (char 16)" in issue_entry.translation_placeholders["error"]
) )
files = await hass.async_add_executor_job( files = await hass.async_add_executor_job(

View file

@ -1,5 +1,6 @@
"""Test Home Assistant json utility functions.""" """Test Home Assistant json utility functions."""
from pathlib import Path from pathlib import Path
import re
import orjson import orjson
import pytest import pytest
@ -21,11 +22,11 @@ TEST_BAD_SERIALIED = "THIS IS NOT JSON\n"
def test_load_bad_data(tmp_path: Path) -> None: def test_load_bad_data(tmp_path: Path) -> None:
"""Test error from trying to load unserialisable data.""" """Test error from trying to load unserializable data."""
fname = tmp_path / "test5.json" fname = tmp_path / "test5.json"
with open(fname, "w") as fh: with open(fname, "w") as fh:
fh.write(TEST_BAD_SERIALIED) fh.write(TEST_BAD_SERIALIED)
with pytest.raises(HomeAssistantError) as err: with pytest.raises(HomeAssistantError, match=re.escape(str(fname))) as err:
load_json(fname) load_json(fname)
assert isinstance(err.value.__cause__, ValueError) assert isinstance(err.value.__cause__, ValueError)
@ -33,7 +34,7 @@ def test_load_bad_data(tmp_path: Path) -> None:
def test_load_json_os_error() -> None: def test_load_json_os_error() -> None:
"""Test trying to load JSON data from a directory.""" """Test trying to load JSON data from a directory."""
fname = "/" fname = "/"
with pytest.raises(HomeAssistantError) as err: with pytest.raises(HomeAssistantError, match=re.escape(str(fname))) as err:
load_json(fname) load_json(fname)
assert isinstance(err.value.__cause__, OSError) assert isinstance(err.value.__cause__, OSError)