1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Adds guards for missing information in call stack frames (#27217)

This commit is contained in:
Franck Nijhof
2019-10-05 11:59:34 +02:00
committed by Pascal Vizeli
parent 71a3516053
commit 2e17ad86af
5 changed files with 34 additions and 12 deletions

View File

@@ -130,7 +130,15 @@ def catch_log_exception(
"""Decorate a callback to catch and log exceptions."""
def log_exception(*args: Any) -> None:
module_name = inspect.getmodule(inspect.trace()[1][0]).__name__ # type: ignore
module = inspect.getmodule(inspect.stack()[1][0])
if module is not None:
module_name = module.__name__
else:
# If Python is unable to access the sources files, the call stack frame
# will be missing information, so let's guard.
# https://github.com/home-assistant/home-assistant/issues/24982
module_name = __name__
# Do not print the wrapper in the traceback
frames = len(inspect.trace()) - 1
exc_msg = traceback.format_exc(-frames)
@@ -178,9 +186,15 @@ def catch_log_coro_exception(
try:
return await target
except Exception: # pylint: disable=broad-except
module_name = inspect.getmodule( # type: ignore
inspect.trace()[1][0]
).__name__
module = inspect.getmodule(inspect.stack()[1][0])
if module is not None:
module_name = module.__name__
else:
# If Python is unable to access the sources files, the frame
# will be missing information, so let's guard.
# https://github.com/home-assistant/home-assistant/issues/24982
module_name = __name__
# Do not print the wrapper in the traceback
frames = len(inspect.trace()) - 1
exc_msg = traceback.format_exc(-frames)