From d1388080c454656393bbb330e3c9babbfc4b24d2 Mon Sep 17 00:00:00 2001 From: Arksine Date: Wed, 6 Jan 2021 12:32:26 -0500 Subject: [PATCH] moonraker: don't crash the server if an optional plugin fails to import The server can still operate and be used to fetch the logs if a plugin fails to load. Add a 'failed_plugins' field to the /server/info response so clients can notify users that this plugin failed to load. Signed-off-by: Eric Callahan --- moonraker/moonraker.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/moonraker/moonraker.py b/moonraker/moonraker.py index 31a584f..28e5577 100755 --- a/moonraker/moonraker.py +++ b/moonraker/moonraker.py @@ -64,6 +64,7 @@ class Server: self.init_attempts = 0 self.klippy_state = "disconnected" self.subscriptions = {} + self.failed_plugins = [] # Server/IOLoop self.server_running = False @@ -132,11 +133,12 @@ class Server: if not os.path.exists(mod_path): msg = f"Plugin ({plugin_name}) does not exist" logging.info(msg) + self.failed_plugins.append(plugin_name) if default == Sentinel: raise ServerError(msg) return default - module = importlib.import_module("plugins." + plugin_name) try: + module = importlib.import_module("plugins." + plugin_name) func_name = "load_plugin" if hasattr(module, "load_plugin_multi"): func_name = "load_plugin_multi" @@ -147,6 +149,7 @@ class Server: except Exception: msg = f"Unable to load plugin ({plugin_name})" logging.exception(msg) + self.failed_plugins.append(plugin_name) if default == Sentinel: raise ServerError(msg) return default @@ -464,7 +467,8 @@ class Server: return { 'klippy_connected': self.klippy_connection.is_connected(), 'klippy_state': self.klippy_state, - 'plugins': list(self.plugins.keys())} + 'plugins': list(self.plugins.keys()), + 'failed_plugins': self.failed_plugins} class KlippyConnection: def __init__(self, on_recd, on_close):