mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Move config validation exception logging to bootstrap + humanize
This commit is contained in:
parent
f904d06c9a
commit
9fa1328111
@ -11,12 +11,12 @@ from types import ModuleType
|
|||||||
from typing import Any, Optional, Dict
|
from typing import Any, Optional, Dict
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
from voluptuous.humanize import humanize_error
|
||||||
|
|
||||||
import homeassistant.components as core_components
|
import homeassistant.components as core_components
|
||||||
from homeassistant.components import group, persistent_notification
|
from homeassistant.components import group, persistent_notification
|
||||||
import homeassistant.config as conf_util
|
import homeassistant.config as conf_util
|
||||||
import homeassistant.core as core
|
import homeassistant.core as core
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
import homeassistant.loader as loader
|
import homeassistant.loader as loader
|
||||||
import homeassistant.util.package as pkg_util
|
import homeassistant.util.package as pkg_util
|
||||||
from homeassistant.const import EVENT_COMPONENT_LOADED, PLATFORM_FORMAT
|
from homeassistant.const import EVENT_COMPONENT_LOADED, PLATFORM_FORMAT
|
||||||
@ -103,7 +103,7 @@ def _setup_component(hass: core.HomeAssistant, domain: str, config) -> bool:
|
|||||||
try:
|
try:
|
||||||
config = component.CONFIG_SCHEMA(config)
|
config = component.CONFIG_SCHEMA(config)
|
||||||
except vol.MultipleInvalid as ex:
|
except vol.MultipleInvalid as ex:
|
||||||
cv.log_exception(_LOGGER, ex, domain, config)
|
_log_exception(ex, domain, config)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
elif hasattr(component, 'PLATFORM_SCHEMA'):
|
elif hasattr(component, 'PLATFORM_SCHEMA'):
|
||||||
@ -113,7 +113,7 @@ def _setup_component(hass: core.HomeAssistant, domain: str, config) -> bool:
|
|||||||
try:
|
try:
|
||||||
p_validated = component.PLATFORM_SCHEMA(p_config)
|
p_validated = component.PLATFORM_SCHEMA(p_config)
|
||||||
except vol.MultipleInvalid as ex:
|
except vol.MultipleInvalid as ex:
|
||||||
cv.log_exception(_LOGGER, ex, domain, p_config)
|
_log_exception(ex, domain, p_config)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Not all platform components follow same pattern for platforms
|
# Not all platform components follow same pattern for platforms
|
||||||
@ -134,8 +134,8 @@ def _setup_component(hass: core.HomeAssistant, domain: str, config) -> bool:
|
|||||||
try:
|
try:
|
||||||
p_validated = platform.PLATFORM_SCHEMA(p_validated)
|
p_validated = platform.PLATFORM_SCHEMA(p_validated)
|
||||||
except vol.MultipleInvalid as ex:
|
except vol.MultipleInvalid as ex:
|
||||||
cv.log_exception(_LOGGER, ex, '{}.{}'
|
_log_exception(ex, '{}.{}'.format(domain, p_name),
|
||||||
.format(domain, p_name), p_validated)
|
p_validated)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
platforms.append(p_validated)
|
platforms.append(p_validated)
|
||||||
@ -239,7 +239,7 @@ def from_config_dict(config: Dict[str, Any],
|
|||||||
try:
|
try:
|
||||||
conf_util.process_ha_core_config(hass, core_config)
|
conf_util.process_ha_core_config(hass, core_config)
|
||||||
except vol.Invalid as ex:
|
except vol.Invalid as ex:
|
||||||
cv.log_exception(_LOGGER, ex, 'homeassistant', core_config)
|
_log_exception(ex, 'homeassistant', core_config)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
conf_util.process_ha_config_upgrade(hass)
|
conf_util.process_ha_config_upgrade(hass)
|
||||||
@ -374,3 +374,20 @@ def _ensure_loader_prepared(hass: core.HomeAssistant) -> None:
|
|||||||
def _mount_local_lib_path(config_dir: str) -> None:
|
def _mount_local_lib_path(config_dir: str) -> None:
|
||||||
"""Add local library to Python Path."""
|
"""Add local library to Python Path."""
|
||||||
sys.path.insert(0, os.path.join(config_dir, 'deps'))
|
sys.path.insert(0, os.path.join(config_dir, 'deps'))
|
||||||
|
|
||||||
|
|
||||||
|
def _log_exception(ex, domain, config):
|
||||||
|
"""Generate log exception for config validation."""
|
||||||
|
message = 'Invalid config for [{}]: '.format(domain)
|
||||||
|
if 'extra keys not allowed' in ex.error_message:
|
||||||
|
message += '[{}] is an invalid option for [{}]. Check: {}->{}.'\
|
||||||
|
.format(ex.path[-1], domain, domain,
|
||||||
|
'->'.join('%s' % m for m in ex.path))
|
||||||
|
else:
|
||||||
|
message += humanize_error(config, ex)
|
||||||
|
|
||||||
|
if hasattr(config, '__line__'):
|
||||||
|
message += " (See {}:{})".format(config.__config_file__,
|
||||||
|
config.__line__ or '?')
|
||||||
|
|
||||||
|
_LOGGER.error(message)
|
||||||
|
@ -152,23 +152,6 @@ def time_period_str(value: str) -> timedelta:
|
|||||||
time_period = vol.Any(time_period_str, timedelta, time_period_dict)
|
time_period = vol.Any(time_period_str, timedelta, time_period_dict)
|
||||||
|
|
||||||
|
|
||||||
def log_exception(logger, ex, domain, config):
|
|
||||||
"""Generate log exception for config validation."""
|
|
||||||
message = 'Invalid config for [{}]: '.format(domain)
|
|
||||||
if 'extra keys not allowed' in ex.error_message:
|
|
||||||
message += '[{}] is an invalid option for [{}]. Check: {}->{}.'\
|
|
||||||
.format(ex.path[-1], domain, domain,
|
|
||||||
'->'.join('%s' % m for m in ex.path))
|
|
||||||
else:
|
|
||||||
message += str(ex)
|
|
||||||
|
|
||||||
if hasattr(config, '__line__'):
|
|
||||||
message += " (See {}:{})".format(config.__config_file__,
|
|
||||||
config.__line__ or '?')
|
|
||||||
|
|
||||||
logger.error(message)
|
|
||||||
|
|
||||||
|
|
||||||
def match_all(value):
|
def match_all(value):
|
||||||
"""Validator that matches all values."""
|
"""Validator that matches all values."""
|
||||||
return value
|
return value
|
||||||
|
Loading…
x
Reference in New Issue
Block a user