mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-07 07:48:50 +00:00
add custom errors for plugin
This commit is contained in:
parent
a3410a30d5
commit
0283214034
@ -7,9 +7,13 @@ from .plugin import InvenTreePlugin
|
|||||||
from .integration import IntegrationPluginBase
|
from .integration import IntegrationPluginBase
|
||||||
from .action import ActionPlugin
|
from .action import ActionPlugin
|
||||||
|
|
||||||
|
from .helpers import MixinNotImplementedError, MixinImplementationError
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'ActionPlugin',
|
'ActionPlugin',
|
||||||
'IntegrationPluginBase',
|
'IntegrationPluginBase',
|
||||||
'InvenTreePlugin',
|
'InvenTreePlugin',
|
||||||
'plugin_registry',
|
'plugin_registry',
|
||||||
|
'MixinNotImplementedError',
|
||||||
|
'MixinImplementationError',
|
||||||
]
|
]
|
||||||
|
@ -11,6 +11,7 @@ from django.db.utils import OperationalError, ProgrammingError
|
|||||||
|
|
||||||
from plugin.models import PluginConfig, PluginSetting
|
from plugin.models import PluginConfig, PluginSetting
|
||||||
from plugin.urls import PLUGIN_BASE
|
from plugin.urls import PLUGIN_BASE
|
||||||
|
from plugin.helpers import MixinImplementationError, MixinNotImplementedError
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger('inventree')
|
logger = logging.getLogger('inventree')
|
||||||
@ -105,24 +106,24 @@ class ScheduleMixin:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if not self.has_scheduled_tasks:
|
if not self.has_scheduled_tasks:
|
||||||
raise ValueError("SCHEDULED_TASKS not defined")
|
raise MixinImplementationError("SCHEDULED_TASKS not defined")
|
||||||
|
|
||||||
for key, task in self.scheduled_tasks.items():
|
for key, task in self.scheduled_tasks.items():
|
||||||
|
|
||||||
if 'func' not in task:
|
if 'func' not in task:
|
||||||
raise ValueError(f"Task '{key}' is missing 'func' parameter")
|
raise MixinImplementationError(f"Task '{key}' is missing 'func' parameter")
|
||||||
|
|
||||||
if 'schedule' not in task:
|
if 'schedule' not in task:
|
||||||
raise ValueError(f"Task '{key}' is missing 'schedule' parameter")
|
raise MixinImplementationError(f"Task '{key}' is missing 'schedule' parameter")
|
||||||
|
|
||||||
schedule = task['schedule'].upper().strip()
|
schedule = task['schedule'].upper().strip()
|
||||||
|
|
||||||
if schedule not in self.ALLOWABLE_SCHEDULE_TYPES:
|
if schedule not in self.ALLOWABLE_SCHEDULE_TYPES:
|
||||||
raise ValueError(f"Task '{key}': Schedule '{schedule}' is not a valid option")
|
raise MixinImplementationError(f"Task '{key}': Schedule '{schedule}' is not a valid option")
|
||||||
|
|
||||||
# If 'minutes' is selected, it must be provided!
|
# If 'minutes' is selected, it must be provided!
|
||||||
if schedule == 'I' and 'minutes' not in task:
|
if schedule == 'I' and 'minutes' not in task:
|
||||||
raise ValueError(f"Task '{key}' is missing 'minutes' parameter")
|
raise MixinImplementationError(f"Task '{key}' is missing 'minutes' parameter")
|
||||||
|
|
||||||
def get_task_name(self, key):
|
def get_task_name(self, key):
|
||||||
# Generate a 'unique' task name
|
# Generate a 'unique' task name
|
||||||
@ -192,7 +193,7 @@ class EventMixin:
|
|||||||
|
|
||||||
def process_event(self, event, *args, **kwargs):
|
def process_event(self, event, *args, **kwargs):
|
||||||
# Default implementation does not do anything
|
# Default implementation does not do anything
|
||||||
raise NotImplementedError
|
raise MixinNotImplementedError
|
||||||
|
|
||||||
class MixinMeta:
|
class MixinMeta:
|
||||||
MIXIN_NAME = 'Events'
|
MIXIN_NAME = 'Events'
|
||||||
@ -280,7 +281,7 @@ class NavigationMixin:
|
|||||||
# check if needed values are configured
|
# check if needed values are configured
|
||||||
for link in nav_links:
|
for link in nav_links:
|
||||||
if False in [a in link for a in ('link', 'name', )]:
|
if False in [a in link for a in ('link', 'name', )]:
|
||||||
raise NotImplementedError('Wrong Link definition', link)
|
raise MixinNotImplementedError('Wrong Link definition', link)
|
||||||
return nav_links
|
return nav_links
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -29,6 +29,21 @@ class IntegrationPluginError(Exception):
|
|||||||
return self.message
|
return self.message
|
||||||
|
|
||||||
|
|
||||||
|
class MixinImplementationError(ValueError):
|
||||||
|
"""
|
||||||
|
Error if mixin was implemented wrong in plugin
|
||||||
|
Mostly raised if constant is missing
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class MixinNotImplementedError(NotImplementedError):
|
||||||
|
"""
|
||||||
|
Error if necessary mixin function was not overwritten
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def get_plugin_error(error, do_raise: bool = False, do_log: bool = False, log_name: str = ''):
|
def get_plugin_error(error, do_raise: bool = False, do_log: bool = False, log_name: str = ''):
|
||||||
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"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user