From a78764f000ccadb499a6bf2c34243031497b9490 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 17 Nov 2023 14:57:37 +0100 Subject: [PATCH] Improve formatting of package errors (#104078) --- homeassistant/config.py | 23 ++++++++++------------- homeassistant/helpers/check_config.py | 2 +- tests/helpers/test_check_config.py | 4 ++-- tests/snapshots/test_config.ambr | 24 ++++++++++++------------ 4 files changed, 25 insertions(+), 28 deletions(-) diff --git a/homeassistant/config.py b/homeassistant/config.py index 027839ca656..ac68f03ff52 100644 --- a/homeassistant/config.py +++ b/homeassistant/config.py @@ -831,17 +831,11 @@ def _log_pkg_error( hass: HomeAssistant, package: str, component: str, config: dict, message: str ) -> None: """Log an error while merging packages.""" - message = f"Package {package} setup failed. {message}" + message_prefix = f"Setup of package '{package}'" + if annotation := find_annotation(config, [CONF_CORE, CONF_PACKAGES, package]): + message_prefix += f" at {_relpath(hass, annotation[0])}, line {annotation[1]}" - pack_config = config[CONF_CORE][CONF_PACKAGES].get(package, config) - config_file = getattr(pack_config, "__config_file__", None) - if config_file: - config_file = _relpath(hass, config_file) - else: - config_file = "?" - message += f" (See {config_file}:{getattr(pack_config, '__line__', '?')})." - - _LOGGER.error(message) + _LOGGER.error("%s failed: %s", message_prefix, message) def _identify_config_schema(module: ComponentProtocol) -> str | None: @@ -985,7 +979,7 @@ async def merge_packages_config( pack_name, comp_name, config, - f"Integration {comp_name} cannot be merged. Expected a dict.", + f"integration '{comp_name}' cannot be merged, expected a dict", ) continue @@ -998,7 +992,10 @@ async def merge_packages_config( pack_name, comp_name, config, - f"Integration {comp_name} cannot be merged. Dict expected in main config.", + ( + f"integration '{comp_name}' cannot be merged, dict expected in " + "main config" + ), ) continue @@ -1009,7 +1006,7 @@ async def merge_packages_config( pack_name, comp_name, config, - f"Integration {comp_name} has duplicate key '{duplicate_key}'.", + f"integration '{comp_name}' has duplicate key '{duplicate_key}'", ) return config diff --git a/homeassistant/helpers/check_config.py b/homeassistant/helpers/check_config.py index 09bdf10cf70..ea5e7218f1f 100644 --- a/homeassistant/helpers/check_config.py +++ b/homeassistant/helpers/check_config.py @@ -100,7 +100,7 @@ async def async_check_ha_config_file( # noqa: C901 message: str, ) -> None: """Handle errors from packages.""" - message = f"Package {package} setup failed. {message}" + message = f"Setup of package '{package}' failed: {message}" domain = f"homeassistant.packages.{package}.{component}" pack_config = core_config[CONF_PACKAGES].get(package, config) result.add_warning(message, domain, pack_config) diff --git a/tests/helpers/test_check_config.py b/tests/helpers/test_check_config.py index 56df99bb032..1dd07bc66ac 100644 --- a/tests/helpers/test_check_config.py +++ b/tests/helpers/test_check_config.py @@ -379,8 +379,8 @@ async def test_package_invalid(hass: HomeAssistant) -> None: warning = CheckConfigError( ( - "Package p1 setup failed. Integration group cannot be merged. Expected a " - "dict." + "Setup of package 'p1' failed: integration 'group' cannot be merged" + ", expected a dict" ), "homeassistant.packages.p1.group", {"group": ["a"]}, diff --git a/tests/snapshots/test_config.ambr b/tests/snapshots/test_config.ambr index b65617cb649..785989ad839 100644 --- a/tests/snapshots/test_config.ambr +++ b/tests/snapshots/test_config.ambr @@ -312,38 +312,38 @@ # --- # name: test_package_merge_error[packages] list([ - 'Package pack_1 setup failed. Integration adr_0007_1 cannot be merged. Dict expected in main config. (See configuration.yaml:9).', - 'Package pack_2 setup failed. Integration adr_0007_2 cannot be merged. Expected a dict. (See configuration.yaml:13).', - "Package pack_4 setup failed. Integration adr_0007_3 has duplicate key 'host'. (See configuration.yaml:20).", - "Package pack_5 setup failed. Integration 'unknown_integration' not found. (See configuration.yaml:23).", + "Setup of package 'pack_1' at configuration.yaml, line 7 failed: integration 'adr_0007_1' cannot be merged, dict expected in main config", + "Setup of package 'pack_2' at configuration.yaml, line 11 failed: integration 'adr_0007_2' cannot be merged, expected a dict", + "Setup of package 'pack_4' at configuration.yaml, line 19 failed: integration 'adr_0007_3' has duplicate key 'host'", + "Setup of package 'pack_5' at configuration.yaml, line 22 failed: Integration 'unknown_integration' not found.", ]) # --- # name: test_package_merge_error[packages_include_dir_named] list([ - 'Package adr_0007_1 setup failed. Integration adr_0007_1 cannot be merged. Dict expected in main config. (See integrations/adr_0007_1.yaml:2).', - 'Package adr_0007_2 setup failed. Integration adr_0007_2 cannot be merged. Expected a dict. (See integrations/adr_0007_2.yaml:2).', - "Package adr_0007_3_2 setup failed. Integration adr_0007_3 has duplicate key 'host'. (See integrations/adr_0007_3_2.yaml:1).", - "Package unknown_integration setup failed. Integration 'unknown_integration' not found. (See integrations/unknown_integration.yaml:2).", + "Setup of package 'adr_0007_1' at integrations/adr_0007_1.yaml, line 2 failed: integration 'adr_0007_1' cannot be merged, dict expected in main config", + "Setup of package 'adr_0007_2' at integrations/adr_0007_2.yaml, line 2 failed: integration 'adr_0007_2' cannot be merged, expected a dict", + "Setup of package 'adr_0007_3_2' at integrations/adr_0007_3_2.yaml, line 1 failed: integration 'adr_0007_3' has duplicate key 'host'", + "Setup of package 'unknown_integration' at integrations/unknown_integration.yaml, line 2 failed: Integration 'unknown_integration' not found.", ]) # --- # name: test_package_merge_exception[packages-error0] list([ - "Package pack_1 setup failed. Integration test_domain caused error: No such file or directory: b'liblibc.a' (See configuration.yaml:4).", + "Setup of package 'pack_1' at configuration.yaml, line 3 failed: Integration test_domain caused error: No such file or directory: b'liblibc.a'", ]) # --- # name: test_package_merge_exception[packages-error1] list([ - "Package pack_1 setup failed. Integration test_domain caused error: ModuleNotFoundError: No module named 'not_installed_something' (See configuration.yaml:4).", + "Setup of package 'pack_1' at configuration.yaml, line 3 failed: Integration test_domain caused error: ModuleNotFoundError: No module named 'not_installed_something'", ]) # --- # name: test_package_merge_exception[packages_include_dir_named-error0] list([ - "Package unknown_integration setup failed. Integration test_domain caused error: No such file or directory: b'liblibc.a' (See integrations/unknown_integration.yaml:1).", + "Setup of package 'unknown_integration' at integrations/unknown_integration.yaml, line 1 failed: Integration test_domain caused error: No such file or directory: b'liblibc.a'", ]) # --- # name: test_package_merge_exception[packages_include_dir_named-error1] list([ - "Package unknown_integration setup failed. Integration test_domain caused error: ModuleNotFoundError: No module named 'not_installed_something' (See integrations/unknown_integration.yaml:1).", + "Setup of package 'unknown_integration' at integrations/unknown_integration.yaml, line 1 failed: Integration test_domain caused error: ModuleNotFoundError: No module named 'not_installed_something'", ]) # --- # name: test_yaml_error[basic]