From a027a015353931c1657f942e91846fcf509f672f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 19 Jun 2023 06:10:39 -0500 Subject: [PATCH] Log a traceback when importing a component fails (#94778) `2023-06-17 12:44:37.961 ERROR (MainThread) [homeassistant.setup] Setup failed for switchbot: Unable to import component: cannot import name DEFAULT_CIPHERS from urllib3.util.ssl_ (/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/urllib3/util/ssl_.py)` is not very helpful as it does not show which module tried to import. adding a traceback makes it more obvious, and since ImportError is usually not something the user can easily solve, it makes issue reports much more helpful ``` DEFAULT_CIPHERS from urllib3.util.ssl_ (/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/urllib3/util/ssl_.py) Traceback (most recent call last): File "/Users/bdraco/home-assistant/homeassistant/setup.py", line 213, in _async_setup_component component = integration.get_component() ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/bdraco/home-assistant/homeassistant/loader.py", line 813, in get_component ComponentProtocol, importlib.import_module(self.pkg_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Cellar/python@3.11/3.11.3/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1206, in _gcd_import File "", line 1178, in _find_and_load File "", line 1149, in _find_and_load_unlocked File "", line 690, in _load_unlocked File "", line 940, in exec_module File "", line 241, in _call_with_frames_removed File "/Users/bdraco/home-assistant/homeassistant/components/switchbot/__init__.py", line 5, in import switchbot File "/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/switchbot/__init__.py", line 22, in from .devices.lock import SwitchbotLock File "/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/switchbot/devices/lock.py", line 12, in import boto3 File "/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/boto3/__init__.py", line 16, in from boto3.session import Session File "/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/boto3/session.py", line 17, in import botocore.session File "/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/botocore/session.py", line 29, in import botocore.credentials File "/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/botocore/credentials.py", line 34, in from botocore.config import Config File "/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/botocore/config.py", line 16, in from botocore.endpoint import DEFAULT_TIMEOUT, MAX_POOL_CONNECTIONS File "/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/botocore/endpoint.py", line 22, in from botocore.awsrequest import create_request_object File "/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/botocore/awsrequest.py", line 24, in import botocore.utils File "/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/botocore/utils.py", line 32, in import botocore.httpsession File "/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/botocore/httpsession.py", line 10, in from urllib3.util.ssl_ import ( ImportError: cannot import name DEFAULT_CIPHERS from urllib3.util.ssl_ (/Users/bdraco/home-assistant/venv/lib/python3.11/site-packages/urllib3/util/ssl_.py) ``` --- homeassistant/setup.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/homeassistant/setup.py b/homeassistant/setup.py index 2adc5fe1024..bf405d5deda 100644 --- a/homeassistant/setup.py +++ b/homeassistant/setup.py @@ -174,7 +174,7 @@ async def _async_setup_component( """ integration: loader.Integration | None = None - def log_error(msg: str) -> None: + def log_error(msg: str, exc_info: Exception | None = None) -> None: """Log helper.""" if integration is None: custom = "" @@ -182,7 +182,9 @@ async def _async_setup_component( else: custom = "" if integration.is_built_in else "custom integration " link = integration.documentation - _LOGGER.error("Setup failed for %s%s: %s", custom, domain, msg) + _LOGGER.error( + "Setup failed for %s%s: %s", custom, domain, msg, exc_info=exc_info + ) async_notify_setup_error(hass, domain, link) try: @@ -212,7 +214,7 @@ async def _async_setup_component( try: component = integration.get_component() except ImportError as err: - log_error(f"Unable to import component: {err}") + log_error(f"Unable to import component: {err}", err) return False processed_config = await conf_util.async_process_component_config(