Skip to content

Commit

Permalink
Refer to domain configuration in custom validator errors (home-assist…
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored Nov 16, 2023
1 parent cf985a8 commit b400b33
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
16 changes: 12 additions & 4 deletions homeassistant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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}."

Expand Down
2 changes: 1 addition & 1 deletion homeassistant/helpers/check_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _comp_error(
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:
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/test_check_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ async def test_automation_config_platform(hass: HomeAssistant) -> None:
HomeAssistantError("Broken"),
0,
1,
"Invalid config for [bla]: Broken",
"Invalid config for [bla] at configuration.yaml, line 11: Broken",
),
],
)
Expand Down
14 changes: 7 additions & 7 deletions tests/snapshots/test_config.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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',
])
# ---
Expand Down

0 comments on commit b400b33

Please sign in to comment.