Improve formatting of package errors (#104078)

This commit is contained in:
Erik Montnemery 2023-11-17 14:57:37 +01:00 committed by GitHub
parent 2ff9beb9c9
commit a78764f000
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 28 deletions

View File

@ -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

View File

@ -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)

View File

@ -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"]},

View File

@ -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]