From 0283214034ac4debdd73298e5f3c6419f0aac222 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 11 Jan 2022 00:43:18 +0100 Subject: [PATCH] add custom errors for plugin --- InvenTree/plugin/__init__.py | 4 ++++ InvenTree/plugin/builtin/integration/mixins.py | 15 ++++++++------- InvenTree/plugin/helpers.py | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/InvenTree/plugin/__init__.py b/InvenTree/plugin/__init__.py index 86f65919c4..3c220044b5 100644 --- a/InvenTree/plugin/__init__.py +++ b/InvenTree/plugin/__init__.py @@ -7,9 +7,13 @@ from .plugin import InvenTreePlugin from .integration import IntegrationPluginBase from .action import ActionPlugin +from .helpers import MixinNotImplementedError, MixinImplementationError + __all__ = [ 'ActionPlugin', 'IntegrationPluginBase', 'InvenTreePlugin', 'plugin_registry', + 'MixinNotImplementedError', + 'MixinImplementationError', ] diff --git a/InvenTree/plugin/builtin/integration/mixins.py b/InvenTree/plugin/builtin/integration/mixins.py index 586fc8a666..684e100d81 100644 --- a/InvenTree/plugin/builtin/integration/mixins.py +++ b/InvenTree/plugin/builtin/integration/mixins.py @@ -11,6 +11,7 @@ from django.db.utils import OperationalError, ProgrammingError from plugin.models import PluginConfig, PluginSetting from plugin.urls import PLUGIN_BASE +from plugin.helpers import MixinImplementationError, MixinNotImplementedError logger = logging.getLogger('inventree') @@ -105,24 +106,24 @@ class ScheduleMixin: """ 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(): 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: - raise ValueError(f"Task '{key}' is missing 'schedule' parameter") + raise MixinImplementationError(f"Task '{key}' is missing 'schedule' parameter") schedule = task['schedule'].upper().strip() 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 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): # Generate a 'unique' task name @@ -192,7 +193,7 @@ class EventMixin: def process_event(self, event, *args, **kwargs): # Default implementation does not do anything - raise NotImplementedError + raise MixinNotImplementedError class MixinMeta: MIXIN_NAME = 'Events' @@ -280,7 +281,7 @@ class NavigationMixin: # check if needed values are configured for link in nav_links: 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 @property diff --git a/InvenTree/plugin/helpers.py b/InvenTree/plugin/helpers.py index fb46df8927..e6e1e9134d 100644 --- a/InvenTree/plugin/helpers.py +++ b/InvenTree/plugin/helpers.py @@ -29,6 +29,21 @@ class IntegrationPluginError(Exception): 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 = ''): package_path = traceback.extract_tb(error.__traceback__)[-1].filename install_path = sysconfig.get_paths()["purelib"]