From 008917fdef1a582ca822a0a66f1b2766b8e12769 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 20 Nov 2021 13:20:08 +0100 Subject: [PATCH] refactor custom error raising --- InvenTree/plugin/apps.py | 17 ++++------------- InvenTree/plugin/helpers.py | 14 +++++++++++++- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/InvenTree/plugin/apps.py b/InvenTree/plugin/apps.py index a99acc5193..2d3d1d6181 100644 --- a/InvenTree/plugin/apps.py +++ b/InvenTree/plugin/apps.py @@ -24,21 +24,12 @@ from maintenance_mode.core import get_maintenance_mode, set_maintenance_mode from plugin import plugins as inventree_plugins from plugin.integration import IntegrationPluginBase -from plugin.helpers import get_plugin_error +from plugin.helpers import get_plugin_error, IntegrationPluginError logger = logging.getLogger('inventree') -class PluginLoadingError(Exception): - def __init__(self, path, message): - self.path = path - self.message = message - - def __str__(self): - return self.message - - class PluginAppConfig(AppConfig): name = 'plugin' @@ -69,7 +60,7 @@ class PluginAppConfig(AppConfig): except (OperationalError, ProgrammingError): # Exception if the database has not been migrated yet logger.info('Database not accessible while loading plugins') - except PluginLoadingError as error: + except IntegrationPluginError as error: logger.error(f'Encountered an error with {error.path}:\n{error.message}') log_plugin_error({error.path: error.message}, 'load') blocked_plugin = error.path # we will not try to load this app again @@ -140,7 +131,7 @@ class PluginAppConfig(AppConfig): :param disabled: loading path of disabled app, defaults to None :type disabled: str, optional - :raises error: PluginLoadingError + :raises error: IntegrationPluginError """ from plugin.helpers import log_plugin_error from plugin.models import PluginConfig @@ -415,6 +406,6 @@ class PluginAppConfig(AppConfig): cmd(*args, **kwargs) return True, [] except Exception as error: - raise PluginLoadingError(get_plugin_error(error)) + get_plugin_error(error, do_raise=True) # endregion # endregion diff --git a/InvenTree/plugin/helpers.py b/InvenTree/plugin/helpers.py index 46c92bb039..3c1edd82fd 100644 --- a/InvenTree/plugin/helpers.py +++ b/InvenTree/plugin/helpers.py @@ -14,8 +14,20 @@ def log_plugin_error(error, reference: str = 'general'): # add error to stack settings.INTEGRATION_ERRORS[reference].append(error) -def get_plugin_error(error): + +class IntegrationPluginError(Exception): + def __init__(self, path, message): + self.path = path + self.message = message + + def __str__(self): + return self.message + + +def get_plugin_error(error, do_raise: bool = False): package_path = traceback.extract_tb(error.__traceback__)[-1].filename install_path = sysconfig.get_paths()["purelib"] package_name = pathlib.Path(package_path).relative_to(install_path).parts[0] + if do_raise: + raise IntegrationPluginError(package_name, str(error)) return package_name, str(error)