Skip to content

Commit 8b6630c

Browse files
committed
fix(terminal): handle exceptions in command safety check and improve UI feedback
- Add exception handling to the command safety check to prevent crashes - Display an error message if safety check fails - Show a running icon while the command is being executed - Clear the running icon after execution, regardless of success or failure
1 parent f0cbbca commit 8b6630c

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

exts/ext-terminal/src/main/kotlin/cc/unitmesh/terminal/sketch/TerminalSketchProvider.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,12 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
260260
codeSketch.updateViewText(code, true)
261261
titleLabel.text = "Terminal - ($content)"
262262

263-
val (isDangerous, reason) = ShellSyntaxSafetyCheck.checkDangerousCommand(project, content)
263+
val (isDangerous, reason) = try {
264+
ShellSyntaxSafetyCheck.checkDangerousCommand(project, content)
265+
} catch (e: Exception) {
266+
Pair(true, "Error checking command safety: ${e.message}")
267+
}
268+
264269
if (isDangerous) {
265270
AutoDevNotifications.notify(project, "Auto-execution has been disabled for safety: $reason")
266271

@@ -311,6 +316,8 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
311316
inner class TerminalExecuteAction :
312317
AnAction("Execute", AutoDevBundle.message("sketch.terminal.execute"), AllIcons.Actions.Execute) {
313318
override fun actionPerformed(e: AnActionEvent) {
319+
titleLabel.icon = AllIcons.RunConfigurations.TestState.Run
320+
314321
val stdWriter = UIUpdatingWriter(
315322
onTextUpdate = { text, complete ->
316323
resultSketch.updateViewText(text, complete)
@@ -329,7 +336,7 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
329336
resultSketch.updateViewText("", true)
330337
stdWriter.setExecuting(true)
331338
setResultStatus(false)
332-
339+
333340
AutoDevCoroutineScope.scope(project).launch {
334341
val executor = ProcessExecutor(project)
335342
try {
@@ -340,13 +347,16 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
340347
if (collapsibleResultPanel.isCollapsed()) {
341348
collapsibleResultPanel.expand()
342349
}
343-
350+
// Clear the running icon.
351+
titleLabel.icon = null
344352
val success = exitCode == 0
345353
setResultStatus(success, if (!success) "Process exited with code $exitCode" else null)
346354
}
347355
} catch (ex: Exception) {
348356
ApplicationManager.getApplication().invokeLater {
349357
stdWriter.setExecuting(false)
358+
// Clear the running icon.
359+
titleLabel.icon = null
350360
resultSketch.updateViewText("${stdWriter.getContent()}\nError: ${ex.message}", true)
351361
setResultStatus(false, ex.message)
352362
}
@@ -364,3 +374,4 @@ class TerminalLangSketch(val project: Project, var content: String) : ExtensionL
364374
}
365375
}
366376
}
377+

0 commit comments

Comments
 (0)