From 9a476073cba92159af26526919836ca165f5eb18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 3 Feb 2015 10:17:39 +0100 Subject: [PATCH] Made call_plugin method more error resilient Should catch any exceptions thrown by the plugin, log them and only hand them off if the caller requested to be informed about such things. This way if something goes wrong with a StartupPlugin there should be no problem anymore to still get the server up and running. --- src/octoprint/plugin/__init__.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/octoprint/plugin/__init__.py b/src/octoprint/plugin/__init__.py index 3b9112e4..90859e1b 100644 --- a/src/octoprint/plugin/__init__.py +++ b/src/octoprint/plugin/__init__.py @@ -6,6 +6,7 @@ __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agp __copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License" import os +import logging from octoprint.settings import settings from octoprint.plugin.core import (PluginInfo, PluginManager, Plugin) @@ -51,7 +52,7 @@ def plugin_settings(plugin_key, defaults=None): return PluginSettings(settings(), plugin_key, defaults=defaults) -def call_plugin(types, method, args=None, kwargs=None, callback=None): +def call_plugin(types, method, args=None, kwargs=None, callback=None, error_callback=None): if not isinstance(types, (list, tuple)): types = [types] if args is None: @@ -62,9 +63,14 @@ def call_plugin(types, method, args=None, kwargs=None, callback=None): plugins = plugin_manager().get_implementations(*types) for name, plugin in plugins.items(): if hasattr(plugin, method): - result = getattr(plugin, method)(*args, **kwargs) - if callback: - callback(name, plugin, result) + try: + result = getattr(plugin, method)(*args, **kwargs) + if callback: + callback(name, plugin, result) + except Exception as e: + logging.getLogger(__name__).exception("Error while calling plugin %s" % name) + if error_callback: + error_callback(name, plugin, e) class PluginSettings(object):