Skip to content

[show][config][plugin] add processing of ModuleNotFoundError with log_warning #3832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/test_util_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from unittest.mock import patch, MagicMock
from utilities_common.util_base import UtilHelper


@patch("pkgutil.iter_modules")
@patch("utilities_common.util_base.log.log_error")
@patch("utilities_common.util_base.log.log_warning")
def test_load_plugins_exceptions_logs(mock_log_warning, mock_log_error, mock_iter_modules):
NON_EXISTENT_MODULE_NAME = "non-existent-module"
FAILED_TO_IMPORT_MESSAGE = f"failed to import plugin {NON_EXISTENT_MODULE_NAME}"
COMMON_EXCEPTION_MESSAGE = "Common exception"

mock_iter_modules.return_value = [(None, NON_EXISTENT_MODULE_NAME, False)]
plugins_namespace = MagicMock()
plugins_namespace.__path__ = "some_path"
plugins_namespace.__name__ = "some_name"

# Assetion for ModuleNotFoundError
list(UtilHelper().load_plugins(plugins_namespace))
mock_log_warning.assert_called_once_with(
f"{FAILED_TO_IMPORT_MESSAGE}: No module named '{NON_EXISTENT_MODULE_NAME}'",
also_print_to_console=True
)

# Assertion for Exception
with patch("importlib.import_module", side_effect=Exception(COMMON_EXCEPTION_MESSAGE)):
list(UtilHelper().load_plugins(plugins_namespace))
mock_log_error.assert_called_once_with(
f"{FAILED_TO_IMPORT_MESSAGE}: {COMMON_EXCEPTION_MESSAGE}",
also_print_to_console=True
)
6 changes: 6 additions & 0 deletions utilities_common/util_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ def iter_namespace(ns_pkg):
log.log_debug('importing plugin: {}'.format(module_name))
try:
module = importlib.import_module(module_name)

except ModuleNotFoundError as err:
log.log_warning('failed to import plugin {}: {}'.format(module_name, err),
also_print_to_console=True)
continue

except Exception as err:
log.log_error('failed to import plugin {}: {}'.format(module_name, err),
also_print_to_console=True)
Expand Down
Loading