mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-30 04:26:44 +00:00
refactor custom error raising
This commit is contained in:
parent
71e05d569b
commit
008917fdef
@ -24,21 +24,12 @@ 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
|
from plugin.helpers import get_plugin_error, IntegrationPluginError
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger('inventree')
|
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):
|
class PluginAppConfig(AppConfig):
|
||||||
name = 'plugin'
|
name = 'plugin'
|
||||||
|
|
||||||
@ -69,7 +60,7 @@ class PluginAppConfig(AppConfig):
|
|||||||
except (OperationalError, ProgrammingError):
|
except (OperationalError, ProgrammingError):
|
||||||
# Exception if the database has not been migrated yet
|
# Exception if the database has not been migrated yet
|
||||||
logger.info('Database not accessible while loading plugins')
|
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}')
|
logger.error(f'Encountered an error with {error.path}:\n{error.message}')
|
||||||
log_plugin_error({error.path: error.message}, 'load')
|
log_plugin_error({error.path: error.message}, 'load')
|
||||||
blocked_plugin = error.path # we will not try to load this app again
|
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
|
:param disabled: loading path of disabled app, defaults to None
|
||||||
:type disabled: str, optional
|
:type disabled: str, optional
|
||||||
:raises error: PluginLoadingError
|
:raises error: IntegrationPluginError
|
||||||
"""
|
"""
|
||||||
from plugin.helpers import log_plugin_error
|
from plugin.helpers import log_plugin_error
|
||||||
from plugin.models import PluginConfig
|
from plugin.models import PluginConfig
|
||||||
@ -415,6 +406,6 @@ class PluginAppConfig(AppConfig):
|
|||||||
cmd(*args, **kwargs)
|
cmd(*args, **kwargs)
|
||||||
return True, []
|
return True, []
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
raise PluginLoadingError(get_plugin_error(error))
|
get_plugin_error(error, do_raise=True)
|
||||||
# endregion
|
# endregion
|
||||||
# endregion
|
# endregion
|
||||||
|
@ -14,8 +14,20 @@ 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):
|
|
||||||
|
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
|
package_path = traceback.extract_tb(error.__traceback__)[-1].filename
|
||||||
install_path = sysconfig.get_paths()["purelib"]
|
install_path = sysconfig.get_paths()["purelib"]
|
||||||
package_name = pathlib.Path(package_path).relative_to(install_path).parts[0]
|
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)
|
return package_name, str(error)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user