refactor custom error raising

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

View File

@ -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

View File

@ -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)