2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-30 12:36:45 +00:00

refactor plugin error processing definition

This commit is contained in:
Matthias 2021-11-20 13:14:18 +01:00
parent b05381fcc8
commit 71e05d569b
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
2 changed files with 12 additions and 6 deletions

View File

@ -3,8 +3,6 @@ from __future__ import unicode_literals
import importlib import importlib
import pathlib import pathlib
import logging import logging
import sysconfig
import traceback
from typing import OrderedDict from typing import OrderedDict
from importlib import reload 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 import plugins as inventree_plugins
from plugin.integration import IntegrationPluginBase from plugin.integration import IntegrationPluginBase
from plugin.helpers import get_plugin_error
logger = logging.getLogger('inventree') logger = logging.getLogger('inventree')
@ -416,9 +415,6 @@ class PluginAppConfig(AppConfig):
cmd(*args, **kwargs) cmd(*args, **kwargs)
return True, [] return True, []
except Exception as error: except Exception as error:
package_path = traceback.extract_tb(error.__traceback__)[-1].filename raise PluginLoadingError(get_plugin_error(error))
install_path = sysconfig.get_paths()["purelib"]
package_name = pathlib.Path(package_path).relative_to(install_path).parts[0]
raise PluginLoadingError(package_name, str(error))
# endregion # endregion
# endregion # endregion

View File

@ -1,4 +1,8 @@
"""Helpers for plugin app""" """Helpers for plugin app"""
import pathlib
import sysconfig
import traceback
from django.conf import settings from django.conf import settings
@ -9,3 +13,9 @@ def log_plugin_error(error, reference: str = 'general'):
# add error to stack # add error to stack
settings.INTEGRATION_ERRORS[reference].append(error) 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)