Improved error message in group plugin

This commit is contained in:
squidfunk 2023-09-11 17:08:28 +02:00
parent 4154a94956
commit 2e85464401
No known key found for this signature in database
GPG Key ID: 5ED40BC4F9C436DF
2 changed files with 24 additions and 10 deletions

View File

@ -23,6 +23,7 @@ import logging
from collections.abc import Callable from collections.abc import Callable
from mkdocs.config.config_options import Plugins from mkdocs.config.config_options import Plugins
from mkdocs.config.defaults import MkDocsConfig from mkdocs.config.defaults import MkDocsConfig
from mkdocs.exceptions import PluginError
from mkdocs.plugins import BasePlugin, event_priority from mkdocs.plugins import BasePlugin, event_priority
from .config import GroupConfig from .config import GroupConfig
@ -65,8 +66,14 @@ class GroupPlugin(BasePlugin[GroupConfig]):
# Load all plugins in group # Load all plugins in group
self.plugins: dict[str, BasePlugin] = {} self.plugins: dict[str, BasePlugin] = {}
for name, plugin in self._load(option): try:
self.plugins[name] = plugin for name, plugin in self._load(option):
self.plugins[name] = plugin
# The plugin could not be loaded, likely because it's not installed or
# misconfigured, so we raise a plugin error for a nicer error message
except Exception as e:
raise PluginError(str(e))
# Patch order of plugin methods # Patch order of plugin methods
for events in option.plugins.events.values(): for events in option.plugins.events.values():
@ -99,9 +106,9 @@ class GroupPlugin(BasePlugin[GroupConfig]):
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
# Patch order of plugin events - all other plugin methods are already in the # Patch order of plugin methods - all other plugin methods are already in
# right order, so we only need to check those that are part of the group and # the right order, so we only need to check those that are part of the group
# bubble them up into the right location. Some plugin methods may define # and bubble them up into the right location. Some plugin methods may define
# priorities, so we need to make sure to order correctly within those. # priorities, so we need to make sure to order correctly within those.
def _patch(self, methods: list[Callable], config: MkDocsConfig): def _patch(self, methods: list[Callable], config: MkDocsConfig):
position = self._get_position(self, config) position = self._get_position(self, config)

View File

@ -23,6 +23,7 @@ import logging
from collections.abc import Callable from collections.abc import Callable
from mkdocs.config.config_options import Plugins from mkdocs.config.config_options import Plugins
from mkdocs.config.defaults import MkDocsConfig from mkdocs.config.defaults import MkDocsConfig
from mkdocs.exceptions import PluginError
from mkdocs.plugins import BasePlugin, event_priority from mkdocs.plugins import BasePlugin, event_priority
from .config import GroupConfig from .config import GroupConfig
@ -65,8 +66,14 @@ class GroupPlugin(BasePlugin[GroupConfig]):
# Load all plugins in group # Load all plugins in group
self.plugins: dict[str, BasePlugin] = {} self.plugins: dict[str, BasePlugin] = {}
for name, plugin in self._load(option): try:
self.plugins[name] = plugin for name, plugin in self._load(option):
self.plugins[name] = plugin
# The plugin could not be loaded, likely because it's not installed or
# misconfigured, so we raise a plugin error for a nicer error message
except Exception as e:
raise PluginError(str(e))
# Patch order of plugin methods # Patch order of plugin methods
for events in option.plugins.events.values(): for events in option.plugins.events.values():
@ -99,9 +106,9 @@ class GroupPlugin(BasePlugin[GroupConfig]):
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
# Patch order of plugin events - all other plugin methods are already in the # Patch order of plugin methods - all other plugin methods are already in
# right order, so we only need to check those that are part of the group and # the right order, so we only need to check those that are part of the group
# bubble them up into the right location. Some plugin methods may define # and bubble them up into the right location. Some plugin methods may define
# priorities, so we need to make sure to order correctly within those. # priorities, so we need to make sure to order correctly within those.
def _patch(self, methods: list[Callable], config: MkDocsConfig): def _patch(self, methods: list[Callable], config: MkDocsConfig):
position = self._get_position(self, config) position = self._get_position(self, config)