From 4c8acfb8d14e9cec4eb4d4446681a957eeea65cd Mon Sep 17 00:00:00 2001 From: Lev Mishin Date: Sun, 3 Nov 2024 18:50:14 -0800 Subject: [PATCH] feat: added error handling to the command startup sequence, now it will display an error announcing command unavailability --- commands/commandCreateBaseplate/entry.py | 47 +++++++++++++---------- commands/commandCreateBin/entry.py | 48 ++++++++++++++---------- lib/ui/commandUiState.py | 8 ++-- 3 files changed, 59 insertions(+), 44 deletions(-) diff --git a/commands/commandCreateBaseplate/entry.py b/commands/commandCreateBaseplate/entry.py index 53eed0e..f60919e 100644 --- a/commands/commandCreateBaseplate/entry.py +++ b/commands/commandCreateBaseplate/entry.py @@ -99,40 +99,47 @@ INPUTS_VALID = True -def getErrorMessage(): +def getErrorMessage(text = "An unknown error occurred, please validate your inputs and try again"): stackTrace = traceback.format_exc() - return f"An unknonwn error occurred, please validate your inputs and try again:\n{stackTrace}" + return f"{text}:
{stackTrace}" -def showErrorInMessageBox(): +def showErrorInMessageBox(text = "An unknown error occurred, please validate your inputs and try again"): if ui: - ui.messageBox(getErrorMessage(), f"{CMD_NAME} Error") + ui.messageBox(getErrorMessage(text), f"{CMD_NAME} Error") # Executed when add-in is run. def start(): futil.log(f'{CMD_NAME} Command Start Event') - addinConfig = configUtils.readConfig(CONFIG_FOLDER_PATH) + try: + addinConfig = configUtils.readConfig(CONFIG_FOLDER_PATH) - # Create a command Definition. - cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER) + # Create a command Definition. + cmd_def = ui.commandDefinitions.itemById(CMD_ID) + if not cmd_def: + cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER) - # Define an event handler for the command created event. It will be called when the button is clicked. - futil.add_handler(cmd_def.commandCreated, command_created) + # Define an event handler for the command created event. It will be called when the button is clicked. + futil.add_handler(cmd_def.commandCreated, command_created) - # ******** Add a button into the UI so the user can run the command. ******** - # Get the target workspace the button will be created in. - workspace = ui.workspaces.itemById(WORKSPACE_ID) + # ******** Add a button into the UI so the user can run the command. ******** + # Get the target workspace the button will be created in. + workspace = ui.workspaces.itemById(WORKSPACE_ID) - # Get the panel the button will be created in. - panel = workspace.toolbarPanels.itemById(PANEL_ID) + # Get the panel the button will be created in. + panel = workspace.toolbarPanels.itemById(PANEL_ID) - # Create the button command control in the UI after the specified existing command. - control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False) + # Create the button command control in the UI after the specified existing command. + control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False) - # Specify if the command is promoted to the main toolbar. - control.isPromoted = addinConfig['UI'].getboolean('is_promoted') - # control.isPromoted = IS_PROMOTED + # Specify if the command is promoted to the main toolbar. + control.isPromoted = addinConfig['UI'].getboolean('is_promoted') - initUiState() + initUiState() + ui.statusMessage = "" + except Exception as err: + futil.log(f'{CMD_NAME} Error occurred at the start, {err}, {getErrorMessage()}') + ui.statusMessage = f"{CMD_NAME} failed to initialize" + showErrorInMessageBox(f"{CMD_NAME} Critical error occurred at the start, the command will be unavailable, if the issue persists use this link to report it") # Executed when add-in is stopped. diff --git a/commands/commandCreateBin/entry.py b/commands/commandCreateBin/entry.py index 4686423..30c9479 100644 --- a/commands/commandCreateBin/entry.py +++ b/commands/commandCreateBin/entry.py @@ -215,38 +215,46 @@ def initDefaultUiState(): futil.log(f'{CMD_NAME} Failed to restore default values, err: {err}') futil.log(f'{CMD_NAME} UI state initialized') -def getErrorMessage(): +def getErrorMessage(text = "An unknown error occurred, please validate your inputs and try again"): stackTrace = traceback.format_exc() - return f"An unknonwn error occurred, please validate your inputs and try again:\n{stackTrace}" + return f"{text}:
{stackTrace}" -def showErrorInMessageBox(): +def showErrorInMessageBox(text = "An unknown error occurred, please validate your inputs and try again"): if ui: - ui.messageBox(getErrorMessage(), f"{CMD_NAME} Error") + ui.messageBox(getErrorMessage(text), f"{CMD_NAME} Error") # Executed when add-in is run. def start(): - futil.log(f'{CMD_NAME} Command Start Event') - addinConfig = configUtils.readConfig(CONFIG_FOLDER_PATH) + try: + futil.log(f'{CMD_NAME} Command Start Event') + addinConfig = configUtils.readConfig(CONFIG_FOLDER_PATH) - # Create a command Definition. - cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER) + # Create a command Definition. + cmd_def = ui.commandDefinitions.itemById(CMD_ID) + if not cmd_def: + cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER) - # Define an event handler for the command created event. It will be called when the button is clicked. - futil.add_handler(cmd_def.commandCreated, command_created) + # Define an event handler for the command created event. It will be called when the button is clicked. + futil.add_handler(cmd_def.commandCreated, command_created) - # ******** Add a button into the UI so the user can run the command. ******** - # Get the target workspace the button will be created in. - workspace = ui.workspaces.itemById(WORKSPACE_ID) + # ******** Add a button into the UI so the user can run the command. ******** + # Get the target workspace the button will be created in. + workspace = ui.workspaces.itemById(WORKSPACE_ID) - # Get the panel the button will be created in. - panel = workspace.toolbarPanels.itemById(PANEL_ID) + # Get the panel the button will be created in. + panel = workspace.toolbarPanels.itemById(PANEL_ID) - # Create the button command control in the UI after the specified existing command. - control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False) + # Create the button command control in the UI after the specified existing command. + control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False) - # Specify if the command is promoted to the main toolbar. - control.isPromoted = addinConfig['UI'].getboolean('is_promoted') - initDefaultUiState() + # Specify if the command is promoted to the main toolbar. + control.isPromoted = addinConfig['UI'].getboolean('is_promoted') + initDefaultUiState() + ui.statusMessage = "" + except Exception as err: + futil.log(f'{CMD_NAME} Error occurred at the start, {err}, {getErrorMessage()}') + ui.statusMessage = f"{CMD_NAME} failed to initialize" + showErrorInMessageBox(f"{CMD_NAME} Critical error occurred at the start, the command will be unavailable, if the issue persists use this link to report it") # Executed when add-in is stopped. def stop(): diff --git a/lib/ui/commandUiState.py b/lib/ui/commandUiState.py index dd59ea7..93ccaba 100644 --- a/lib/ui/commandUiState.py +++ b/lib/ui/commandUiState.py @@ -2,10 +2,10 @@ from ...lib import fusion360utils as futil class SingleInputState: - def __init__(self, inputId: str, inputValue: any, inutType: str): + def __init__(self, inputId: str, inputValue: any, inputType: str): self.id = inputId self.value = inputValue - self.type = inutType + self.type = inputType def toDict(self): return { @@ -103,7 +103,7 @@ def updateInputFromState(self, input: adsk.core.CommandInput): elif isinstance(input, adsk.core.StringValueCommandInput): input.value = value else: - futil.log(f'{self.commandName} Unknonwn input type: {input.id} [{input.objectType}]') + futil.log(f'{self.commandName} Unknown input type: {input.id} [{input.objectType}]') def getState(self, inputId: str): return self.inputState[inputId].value @@ -111,7 +111,7 @@ def getState(self, inputId: str): def getInput(self, inputId: str): return self.commandInputs[inputId] - def toDict(self, ignoreKeys: [str] = []): + def toDict(self, ignoreKeys: list[str] = []): result = {} for key in self.inputState.keys(): if key not in ignoreKeys: