diff --git a/homeassistant/util/json.py b/homeassistant/util/json.py index 65f93020cc6..3a337cf0e18 100644 --- a/homeassistant/util/json.py +++ b/homeassistant/util/json.py @@ -79,12 +79,12 @@ def load_json( except FileNotFoundError: # This is not a fatal error _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) - raise HomeAssistantError(error) from error + raise HomeAssistantError(f"Error while loading {filename}: {error}") from error except OSError as error: _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 diff --git a/tests/helpers/test_storage.py b/tests/helpers/test_storage.py index 363f6051b96..66dd8c10463 100644 --- a/tests/helpers/test_storage.py +++ b/tests/helpers/test_storage.py @@ -706,8 +706,8 @@ async def test_loading_corrupt_core_file( assert issue_entry.translation_placeholders["storage_key"] == storage_key assert issue_entry.issue_domain == HOMEASSISTANT_DOMAIN 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( @@ -767,8 +767,8 @@ async def test_loading_corrupt_file_known_domain( assert issue_entry.translation_placeholders["storage_key"] == storage_key assert issue_entry.issue_domain == "testdomain" 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( diff --git a/tests/util/test_json.py b/tests/util/test_json.py index ff0f1ed8392..ba07c7cbb6c 100644 --- a/tests/util/test_json.py +++ b/tests/util/test_json.py @@ -1,5 +1,6 @@ """Test Home Assistant json utility functions.""" from pathlib import Path +import re import orjson import pytest @@ -21,11 +22,11 @@ TEST_BAD_SERIALIED = "THIS IS NOT JSON\n" 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" with open(fname, "w") as fh: 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) 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: """Test trying to load JSON data from a directory.""" fname = "/" - with pytest.raises(HomeAssistantError) as err: + with pytest.raises(HomeAssistantError, match=re.escape(str(fname))) as err: load_json(fname) assert isinstance(err.value.__cause__, OSError)