From dead016ed991eb7508a18bd3f97a303e34270103 Mon Sep 17 00:00:00 2001 From: Jelle Spijker Date: Thu, 10 Aug 2023 05:56:24 +0200 Subject: [PATCH] Add error messages to BackendPlugin and enhance exception handling Exception handling in BackendPlugin has been improved by adding user-friendly error messages for various exceptions. Errors during backend plugin start or stop will now trigger a message to the user, providing more context about the cause of the failure. This makes it easier for users to understand and resolve possible issues. In addition, the EngineBackend stop function has been modified to forcibly stop all running backend plugins instead of allowing multiple plugins to run simultaneously. --- cura/BackendPlugin.py | 32 +++++++++++++++---- .../CuraEngineBackend/CuraEngineBackend.py | 2 +- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/cura/BackendPlugin.py b/cura/BackendPlugin.py index be48aa50d2..a92e4fe405 100644 --- a/cura/BackendPlugin.py +++ b/cura/BackendPlugin.py @@ -4,6 +4,7 @@ import subprocess from typing import Optional, List from UM.Logger import Logger +from UM.Message import Message from UM.Settings.AdditionalSettingDefinitionAppender import AdditionalSettingDefinitionsAppender @@ -57,13 +58,25 @@ class BackendPlugin(AdditionalSettingDefinitionsAppender): self._is_running = True return True except PermissionError: - Logger.log("e", f"Couldn't start backend_plugin [{self._plugin_id}]: No permission to execute process.") + Logger.log("e", f"Couldn't start EnginePlugin: {self._plugin_id} No permission to execute process.") + self._showMessage(self.catalog.i18nc("@info:plugin_failed", + f"Couldn't start EnginePlugin: {self._plugin_id}\nNo permission to execute process."), + message_type = Message.MessageType.ERROR) except FileNotFoundError: - Logger.logException("e", f"Unable to find backend_plugin executable [{self._plugin_id}]") + Logger.logException("e", f"Unable to find local EnginePlugin server executable for: {self._plugin_id}") + self._showMessage(self.catalog.i18nc("@info:plugin_failed", + f"Unable to find local EnginePlugin server executable for: {self._plugin_id}"), + message_type = Message.MessageType.ERROR) except BlockingIOError: - Logger.logException("e", f"Couldn't start backend_plugin [{self._plugin_id}]: Resource is temporarily unavailable") + Logger.logException("e", f"Couldn't start EnginePlugin: {self._plugin_id} Resource is temporarily unavailable") + self._showMessage(self.catalog.i18nc("@info:plugin_failed", + f"Couldn't start EnginePlugin: {self._plugin_id}\nResource is temporarily unavailable"), + message_type = Message.MessageType.ERROR) except OSError as e: - Logger.logException("e", f"Couldn't start backend_plugin [{self._plugin_id}]: Operating system is blocking it (antivirus?)") + Logger.logException("e", f"Couldn't start EnginePlugin {self._plugin_id} Operating system is blocking it (antivirus?)") + self._showMessage(self.catalog.i18nc("@info:plugin_failed", + f"Couldn't start EnginePlugin: {self._plugin_id}\nOperating system is blocking it (antivirus?)"), + message_type = Message.MessageType.ERROR) return False def stop(self) -> bool: @@ -75,8 +88,15 @@ class BackendPlugin(AdditionalSettingDefinitionsAppender): self._process.terminate() return_code = self._process.wait() self._is_running = False - Logger.log("d", f"Backend_plugin [{self._plugin_id}] was killed. Received return code {return_code}") + Logger.log("d", f"EnginePlugin: {self._plugin_id} was killed. Received return code {return_code}") return True except PermissionError: - Logger.log("e", "Unable to kill running engine. Access is denied.") + Logger.log("e", f"Unable to kill running EnginePlugin: {self._plugin_id} Access is denied.") + self._showMessage(self.catalog.i18nc("@info:plugin_failed", + f"Unable to kill running EnginePlugin: {self._plugin_id}\nAccess is denied."), + message_type = Message.MessageType.ERROR) return False + + def _showMessage(self, message: str, message_type: Message.MessageType = Message.MessageType.ERROR) -> None: + Message(message, title=self.catalog.i18nc("@info:title", "EnginePlugin"), message_type = message_type).show() + diff --git a/plugins/CuraEngineBackend/CuraEngineBackend.py b/plugins/CuraEngineBackend/CuraEngineBackend.py index ef073e6865..2577d227cc 100755 --- a/plugins/CuraEngineBackend/CuraEngineBackend.py +++ b/plugins/CuraEngineBackend/CuraEngineBackend.py @@ -203,7 +203,7 @@ class CuraEngineBackend(QObject, Backend): backend_plugins = CuraApplication.getInstance().getBackendPlugins() for backend_plugin in backend_plugins: if backend_plugin.isRunning(): - continue + backend_plugin.stop() # Set the port to prevent plugins from using the same one. backend_plugin.setPort(self._last_backend_plugin_port) self._last_backend_plugin_port += 1