From ac49298c8da539606b1f54b25d97c416063dd9c1 Mon Sep 17 00:00:00 2001 From: Johann Kellerman Date: Wed, 1 Mar 2017 06:56:23 +0200 Subject: [PATCH] Log errors when loading yaml (#6257) --- homeassistant/bootstrap.py | 3 ++- homeassistant/config.py | 6 +++++- homeassistant/scripts/check_config.py | 13 ++++++++++--- tests/scripts/test_check_config.py | 12 ++++++++---- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index b1233594f89..db9f4600261 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -428,7 +428,8 @@ def async_from_config_file(config_path: str, try: config_dict = yield from hass.loop.run_in_executor( None, conf_util.load_yaml_config_file, config_path) - except HomeAssistantError: + except HomeAssistantError as err: + _LOGGER.error('Error loading %s: %s', config_path, err) return None finally: clear_secret_cache() diff --git a/homeassistant/config.py b/homeassistant/config.py index 388093ec37a..3968ea571c5 100644 --- a/homeassistant/config.py +++ b/homeassistant/config.py @@ -239,7 +239,11 @@ def load_yaml_config_file(config_path): This method needs to run in an executor. """ - conf_dict = load_yaml(config_path) + try: + conf_dict = load_yaml(config_path) + except FileNotFoundError as err: + raise HomeAssistantError("Config file not found: {}".format( + getattr(err, 'filename', err))) if not isinstance(conf_dict, dict): msg = 'The configuration file {} does not contain a dictionary'.format( diff --git a/homeassistant/scripts/check_config.py b/homeassistant/scripts/check_config.py index 38138c87883..eac0df8bc90 100644 --- a/homeassistant/scripts/check_config.py +++ b/homeassistant/scripts/check_config.py @@ -30,6 +30,8 @@ MOCKS = { config_util.async_log_exception), 'package_error': ("homeassistant.config._log_pkg_error", config_util._log_pkg_error), + 'logger_exception': ("homeassistant.bootstrap._LOGGER.error", + bootstrap._LOGGER.error), } SILENCE = ( 'homeassistant.bootstrap.clear_secret_cache', @@ -180,9 +182,9 @@ def check(config_path): if module is None: # Ensure list - res['except'][ERROR_STR] = res['except'].get(ERROR_STR, []) - res['except'][ERROR_STR].append('{} not found: {}'.format( - 'Platform' if '.' in comp_name else 'Component', comp_name)) + msg = '{} not found: {}'.format( + 'Platform' if '.' in comp_name else 'Component', comp_name) + res['except'].setdefault(ERROR_STR, []).append(msg) return None # Test if platform/component and overwrite setup @@ -224,6 +226,11 @@ def check(config_path): res['except'][pkg_key] = config.get('homeassistant', {}) \ .get('packages', {}).get(package) + def mock_logger_exception(msg, *params): + """Log logger.exceptions.""" + res['except'].setdefault(ERROR_STR, []).append(msg % params) + MOCKS['logger_exception'][1](msg, *params) + # Patches to skip functions for sil in SILENCE: PATCHES[sil] = patch(sil) diff --git a/tests/scripts/test_check_config.py b/tests/scripts/test_check_config.py index 23dde3a8244..63812e1c593 100644 --- a/tests/scripts/test_check_config.py +++ b/tests/scripts/test_check_config.py @@ -85,6 +85,7 @@ class TestCheckConfig(unittest.TestCase): change_yaml_files(res) self.assertDictEqual({}, res['components']) + res['except'].pop(check_config.ERROR_STR) self.assertDictEqual( {'http': {'password': 'err123'}}, res['except'] @@ -111,6 +112,7 @@ class TestCheckConfig(unittest.TestCase): 'light': []}, res['components'] ) + res['except'].pop(check_config.ERROR_STR) self.assertDictEqual( {'light.mqtt_json': {'platform': 'mqtt_json'}}, res['except'] @@ -138,10 +140,12 @@ class TestCheckConfig(unittest.TestCase): res = check_config.check(get_test_config_dir('badplatform.yaml')) change_yaml_files(res) - self.assertDictEqual({'light': []}, res['components']) - self.assertDictEqual({check_config.ERROR_STR: - ['Platform not found: light.beer']}, - res['except']) + assert res['components'] == {'light': []} + assert res['except'] == { + check_config.ERROR_STR: [ + 'Platform not found: light.beer', + 'Unable to find platform light.beer' + ]} self.assertDictEqual({}, res['secret_cache']) self.assertDictEqual({}, res['secrets']) self.assertListEqual(['.../badplatform.yaml'], res['yaml_files'])