From 71e05d569bc611073d9eeb5d0edcf8b86dc0cf9b Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 20 Nov 2021 13:14:18 +0100 Subject: [PATCH] refactor plugin error processing definition --- InvenTree/plugin/apps.py | 8 ++------ InvenTree/plugin/helpers.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/InvenTree/plugin/apps.py b/InvenTree/plugin/apps.py index 4f94a773ac..a99acc5193 100644 --- a/InvenTree/plugin/apps.py +++ b/InvenTree/plugin/apps.py @@ -3,8 +3,6 @@ from __future__ import unicode_literals import importlib import pathlib import logging -import sysconfig -import traceback from typing import OrderedDict from importlib import reload @@ -26,6 +24,7 @@ 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 logger = logging.getLogger('inventree') @@ -416,9 +415,6 @@ class PluginAppConfig(AppConfig): cmd(*args, **kwargs) return True, [] except Exception as error: - 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] - raise PluginLoadingError(package_name, str(error)) + raise PluginLoadingError(get_plugin_error(error)) # endregion # endregion diff --git a/InvenTree/plugin/helpers.py b/InvenTree/plugin/helpers.py index 1ebe146f41..46c92bb039 100644 --- a/InvenTree/plugin/helpers.py +++ b/InvenTree/plugin/helpers.py @@ -1,4 +1,8 @@ """Helpers for plugin app""" +import pathlib +import sysconfig +import traceback + from django.conf import settings @@ -9,3 +13,9 @@ def log_plugin_error(error, reference: str = 'general'): # add error to stack settings.INTEGRATION_ERRORS[reference].append(error) + +def get_plugin_error(error): + 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] + return package_name, str(error)