From 67eebce55a766937af0fc083e8bb3d82fd2bbdfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 5 Jan 2021 14:55:38 +0200 Subject: [PATCH] Better general/fallback error message and traceback for unknown config errors (#44655) * Include error repr in config error message is str(error) yields nothing * Log traceback for config errors we don't have a "friendly" formatter for --- homeassistant/config.py | 11 +++++++---- homeassistant/helpers/check_config.py | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/homeassistant/config.py b/homeassistant/config.py index c3fe18d69df..2da9b0331c9 100644 --- a/homeassistant/config.py +++ b/homeassistant/config.py @@ -412,17 +412,19 @@ def async_log_exception( """ if hass is not None: async_notify_setup_error(hass, domain, link) - _LOGGER.error(_format_config_error(ex, domain, config, link)) + message, is_friendly = _format_config_error(ex, domain, config, link) + _LOGGER.error(message, exc_info=not is_friendly and ex) @callback def _format_config_error( ex: Exception, domain: str, config: Dict, link: Optional[str] = None -) -> str: +) -> Tuple[str, bool]: """Generate log exception for configuration validation. This method must be run in the event loop. """ + is_friendly = False message = f"Invalid config for [{domain}]: " if isinstance(ex, vol.Invalid): if "extra keys not allowed" in ex.error_message: @@ -433,8 +435,9 @@ def _format_config_error( ) else: message += f"{humanize_error(config, ex)}." + is_friendly = True else: - message += str(ex) + message += str(ex) or repr(ex) try: domain_config = config.get(domain, config) @@ -449,7 +452,7 @@ def _format_config_error( if domain != CONF_CORE and link: message += f"Please check the docs at {link}" - return message + return message, is_friendly async def async_process_ha_core_config(hass: HomeAssistant, config: Dict) -> None: diff --git a/homeassistant/helpers/check_config.py b/homeassistant/helpers/check_config.py index c98b563ac7e..97445b8cee2 100644 --- a/homeassistant/helpers/check_config.py +++ b/homeassistant/helpers/check_config.py @@ -78,7 +78,7 @@ async def async_check_ha_config_file(hass: HomeAssistant) -> HomeAssistantConfig def _comp_error(ex: Exception, domain: str, config: ConfigType) -> None: """Handle errors from components: async_log_exception.""" - result.add_error(_format_config_error(ex, domain, config), domain, config) + result.add_error(_format_config_error(ex, domain, config)[0], domain, config) # Load configuration.yaml config_path = hass.config.path(YAML_CONFIG_FILE)