Refer to domain configuration in custom validator errors (#104065)

This commit is contained in:
Erik Montnemery 2023-11-16 15:28:48 +01:00 committed by GitHub
parent cf985a8702
commit b400b33b0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 13 deletions

View File

@ -515,7 +515,7 @@ def async_log_config_validator_error(
if hass is not None:
async_notify_setup_error(hass, domain, link)
message = format_homeassistant_error(ex, domain, config, link)
message = format_homeassistant_error(hass, ex, domain, config, link)
_LOGGER.error(message, exc_info=ex)
@ -677,11 +677,19 @@ def humanize_error(
@callback
def format_homeassistant_error(
ex: HomeAssistantError, domain: str, config: dict, link: str | None = None
hass: HomeAssistant,
ex: HomeAssistantError,
domain: str,
config: dict,
link: str | None = None,
) -> str:
"""Format HomeAssistantError thrown by a custom config validator."""
message = f"Invalid config for [{domain}]: {str(ex) or repr(ex)}"
message_prefix = f"Invalid config for [{domain}]"
# HomeAssistantError raised by custom config validator has no path to the
# offending configuration key, use the domain key as path instead.
if annotation := find_annotation(config, [domain]):
message_prefix += f" at {_relpath(hass, annotation[0])}, line {annotation[1]}"
message = f"{message_prefix}: {str(ex) or repr(ex)}"
if domain != CONF_CORE and link:
message += f" Please check the docs at {link}."

View File

@ -115,7 +115,7 @@ async def async_check_ha_config_file( # noqa: C901
if isinstance(ex, vol.Invalid):
message = format_schema_error(hass, ex, domain, component_config)
else:
message = format_homeassistant_error(ex, domain, component_config)
message = format_homeassistant_error(hass, ex, domain, component_config)
if domain in frontend_dependencies:
result.add_error(message, domain, config_to_attach)
else:

View File

@ -453,7 +453,7 @@ action:
HomeAssistantError("Broken"),
0,
1,
"Invalid config for [bla]: Broken",
"Invalid config for [bla] at configuration.yaml, line 11: Broken",
),
],
)

View File

@ -47,7 +47,7 @@
}),
dict({
'has_exc_info': True,
'message': 'Invalid config for [custom_validator_bad_1]: broken',
'message': 'Invalid config for [custom_validator_bad_1] at configuration.yaml, line 55: broken',
}),
dict({
'has_exc_info': True,
@ -103,7 +103,7 @@
}),
dict({
'has_exc_info': True,
'message': 'Invalid config for [custom_validator_bad_1]: broken',
'message': 'Invalid config for [custom_validator_bad_1] at configuration.yaml, line 9: broken',
}),
dict({
'has_exc_info': True,
@ -135,7 +135,7 @@
}),
dict({
'has_exc_info': True,
'message': 'Invalid config for [custom_validator_bad_1]: broken',
'message': 'Invalid config for [custom_validator_bad_1] at configuration.yaml, line 1: broken',
}),
dict({
'has_exc_info': True,
@ -167,7 +167,7 @@
}),
dict({
'has_exc_info': True,
'message': 'Invalid config for [custom_validator_bad_1]: broken',
'message': 'Invalid config for [custom_validator_bad_1] at configuration.yaml, line 1: broken',
}),
dict({
'has_exc_info': True,
@ -223,7 +223,7 @@
}),
dict({
'has_exc_info': True,
'message': 'Invalid config for [custom_validator_bad_1]: broken',
'message': 'Invalid config for [custom_validator_bad_1] at configuration.yaml, line 67: broken',
}),
dict({
'has_exc_info': True,
@ -279,7 +279,7 @@
}),
dict({
'has_exc_info': True,
'message': 'Invalid config for [custom_validator_bad_1]: broken',
'message': 'Invalid config for [custom_validator_bad_1] at integrations/custom_validator_bad_1.yaml, line 2: broken',
}),
dict({
'has_exc_info': True,
@ -306,7 +306,7 @@
Invalid config for [adr_0007_5] at configuration.yaml, line 45: expected int for dictionary value 'adr_0007_5->port', got 'foo'. Please check the docs at https://www.home-assistant.io/integrations/adr_0007_5.
''',
"Invalid config for [custom_validator_ok_2] at configuration.yaml, line 52: required key 'host' not provided. Please check the docs at https://www.home-assistant.io/integrations/custom_validator_ok_2.",
'Invalid config for [custom_validator_bad_1]: broken Please check the docs at https://www.home-assistant.io/integrations/custom_validator_bad_1.',
'Invalid config for [custom_validator_bad_1] at configuration.yaml, line 55: broken Please check the docs at https://www.home-assistant.io/integrations/custom_validator_bad_1.',
'Unknown error calling custom_validator_bad_2 config validator',
])
# ---