Skip to content

Commit 4c8acfb

Browse files
committed
feat: added error handling to the command startup sequence, now it will display an error announcing command unavailability
1 parent 29de943 commit 4c8acfb

File tree

3 files changed

+59
-44
lines changed

3 files changed

+59
-44
lines changed

commands/commandCreateBaseplate/entry.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -99,40 +99,47 @@
9999

100100
INPUTS_VALID = True
101101

102-
def getErrorMessage():
102+
def getErrorMessage(text = "An unknown error occurred, please validate your inputs and try again"):
103103
stackTrace = traceback.format_exc()
104-
return f"An unknonwn error occurred, please validate your inputs and try again:\n{stackTrace}"
104+
return f"{text}:<br>{stackTrace}"
105105

106-
def showErrorInMessageBox():
106+
def showErrorInMessageBox(text = "An unknown error occurred, please validate your inputs and try again"):
107107
if ui:
108-
ui.messageBox(getErrorMessage(), f"{CMD_NAME} Error")
108+
ui.messageBox(getErrorMessage(text), f"{CMD_NAME} Error")
109109

110110
# Executed when add-in is run.
111111
def start():
112112
futil.log(f'{CMD_NAME} Command Start Event')
113-
addinConfig = configUtils.readConfig(CONFIG_FOLDER_PATH)
113+
try:
114+
addinConfig = configUtils.readConfig(CONFIG_FOLDER_PATH)
114115

115-
# Create a command Definition.
116-
cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER)
116+
# Create a command Definition.
117+
cmd_def = ui.commandDefinitions.itemById(CMD_ID)
118+
if not cmd_def:
119+
cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER)
117120

118-
# Define an event handler for the command created event. It will be called when the button is clicked.
119-
futil.add_handler(cmd_def.commandCreated, command_created)
121+
# Define an event handler for the command created event. It will be called when the button is clicked.
122+
futil.add_handler(cmd_def.commandCreated, command_created)
120123

121-
# ******** Add a button into the UI so the user can run the command. ********
122-
# Get the target workspace the button will be created in.
123-
workspace = ui.workspaces.itemById(WORKSPACE_ID)
124+
# ******** Add a button into the UI so the user can run the command. ********
125+
# Get the target workspace the button will be created in.
126+
workspace = ui.workspaces.itemById(WORKSPACE_ID)
124127

125-
# Get the panel the button will be created in.
126-
panel = workspace.toolbarPanels.itemById(PANEL_ID)
128+
# Get the panel the button will be created in.
129+
panel = workspace.toolbarPanels.itemById(PANEL_ID)
127130

128-
# Create the button command control in the UI after the specified existing command.
129-
control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False)
131+
# Create the button command control in the UI after the specified existing command.
132+
control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False)
130133

131-
# Specify if the command is promoted to the main toolbar.
132-
control.isPromoted = addinConfig['UI'].getboolean('is_promoted')
133-
# control.isPromoted = IS_PROMOTED
134+
# Specify if the command is promoted to the main toolbar.
135+
control.isPromoted = addinConfig['UI'].getboolean('is_promoted')
134136

135-
initUiState()
137+
initUiState()
138+
ui.statusMessage = ""
139+
except Exception as err:
140+
futil.log(f'{CMD_NAME} Error occurred at the start, {err}, {getErrorMessage()}')
141+
ui.statusMessage = f"{CMD_NAME} failed to initialize"
142+
showErrorInMessageBox(f"{CMD_NAME} Critical error occurred at the start, the command will be unavailable, if the issue persists use <a href=\"https://github.com/Le0Michine/FusionGridfinityGenerator/issues/new\">this link</a> to report it")
136143

137144

138145
# Executed when add-in is stopped.

commands/commandCreateBin/entry.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -215,38 +215,46 @@ def initDefaultUiState():
215215
futil.log(f'{CMD_NAME} Failed to restore default values, err: {err}')
216216
futil.log(f'{CMD_NAME} UI state initialized')
217217

218-
def getErrorMessage():
218+
def getErrorMessage(text = "An unknown error occurred, please validate your inputs and try again"):
219219
stackTrace = traceback.format_exc()
220-
return f"An unknonwn error occurred, please validate your inputs and try again:\n{stackTrace}"
220+
return f"{text}:<br>{stackTrace}"
221221

222-
def showErrorInMessageBox():
222+
def showErrorInMessageBox(text = "An unknown error occurred, please validate your inputs and try again"):
223223
if ui:
224-
ui.messageBox(getErrorMessage(), f"{CMD_NAME} Error")
224+
ui.messageBox(getErrorMessage(text), f"{CMD_NAME} Error")
225225

226226
# Executed when add-in is run.
227227
def start():
228-
futil.log(f'{CMD_NAME} Command Start Event')
229-
addinConfig = configUtils.readConfig(CONFIG_FOLDER_PATH)
228+
try:
229+
futil.log(f'{CMD_NAME} Command Start Event')
230+
addinConfig = configUtils.readConfig(CONFIG_FOLDER_PATH)
230231

231-
# Create a command Definition.
232-
cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER)
232+
# Create a command Definition.
233+
cmd_def = ui.commandDefinitions.itemById(CMD_ID)
234+
if not cmd_def:
235+
cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER)
233236

234-
# Define an event handler for the command created event. It will be called when the button is clicked.
235-
futil.add_handler(cmd_def.commandCreated, command_created)
237+
# Define an event handler for the command created event. It will be called when the button is clicked.
238+
futil.add_handler(cmd_def.commandCreated, command_created)
236239

237-
# ******** Add a button into the UI so the user can run the command. ********
238-
# Get the target workspace the button will be created in.
239-
workspace = ui.workspaces.itemById(WORKSPACE_ID)
240+
# ******** Add a button into the UI so the user can run the command. ********
241+
# Get the target workspace the button will be created in.
242+
workspace = ui.workspaces.itemById(WORKSPACE_ID)
240243

241-
# Get the panel the button will be created in.
242-
panel = workspace.toolbarPanels.itemById(PANEL_ID)
244+
# Get the panel the button will be created in.
245+
panel = workspace.toolbarPanels.itemById(PANEL_ID)
243246

244-
# Create the button command control in the UI after the specified existing command.
245-
control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False)
247+
# Create the button command control in the UI after the specified existing command.
248+
control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False)
246249

247-
# Specify if the command is promoted to the main toolbar.
248-
control.isPromoted = addinConfig['UI'].getboolean('is_promoted')
249-
initDefaultUiState()
250+
# Specify if the command is promoted to the main toolbar.
251+
control.isPromoted = addinConfig['UI'].getboolean('is_promoted')
252+
initDefaultUiState()
253+
ui.statusMessage = ""
254+
except Exception as err:
255+
futil.log(f'{CMD_NAME} Error occurred at the start, {err}, {getErrorMessage()}')
256+
ui.statusMessage = f"{CMD_NAME} failed to initialize"
257+
showErrorInMessageBox(f"{CMD_NAME} Critical error occurred at the start, the command will be unavailable, if the issue persists use <a href=\"https://github.com/Le0Michine/FusionGridfinityGenerator/issues/new\">this link</a> to report it")
250258

251259
# Executed when add-in is stopped.
252260
def stop():

lib/ui/commandUiState.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
from ...lib import fusion360utils as futil
33

44
class SingleInputState:
5-
def __init__(self, inputId: str, inputValue: any, inutType: str):
5+
def __init__(self, inputId: str, inputValue: any, inputType: str):
66
self.id = inputId
77
self.value = inputValue
8-
self.type = inutType
8+
self.type = inputType
99

1010
def toDict(self):
1111
return {
@@ -103,15 +103,15 @@ def updateInputFromState(self, input: adsk.core.CommandInput):
103103
elif isinstance(input, adsk.core.StringValueCommandInput):
104104
input.value = value
105105
else:
106-
futil.log(f'{self.commandName} Unknonwn input type: {input.id} [{input.objectType}]')
106+
futil.log(f'{self.commandName} Unknown input type: {input.id} [{input.objectType}]')
107107

108108
def getState(self, inputId: str):
109109
return self.inputState[inputId].value
110110

111111
def getInput(self, inputId: str):
112112
return self.commandInputs[inputId]
113113

114-
def toDict(self, ignoreKeys: [str] = []):
114+
def toDict(self, ignoreKeys: list[str] = []):
115115
result = {}
116116
for key in self.inputState.keys():
117117
if key not in ignoreKeys:

0 commit comments

Comments
 (0)