Skip to content

Commit a104721

Browse files
committed
fix: force window to show when error dialogs pops-up
- this patch makes the window visible in uri handling flows when an error dialog pops-up.
1 parent 1da3f4b commit a104721

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

Diff for: src/main/kotlin/com/coder/toolbox/util/CoderProtocolHandler.kt

+28-19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.coder.toolbox.sdk.v2.models.Workspace
1010
import com.coder.toolbox.sdk.v2.models.WorkspaceAgent
1111
import com.coder.toolbox.sdk.v2.models.WorkspaceStatus
1212
import com.coder.toolbox.settings.CoderSettings
13+
import com.jetbrains.toolbox.api.localization.LocalizableString
1314
import kotlinx.coroutines.TimeoutCancellationException
1415
import kotlinx.coroutines.delay
1516
import kotlinx.coroutines.flow.StateFlow
@@ -48,7 +49,7 @@ open class CoderProtocolHandler(
4849
val deploymentURL = params.url() ?: askUrl()
4950
if (deploymentURL.isNullOrBlank()) {
5051
context.logger.error("Query parameter \"$URL\" is missing from URI $uri")
51-
context.ui.showErrorInfoPopup(MissingArgumentException("Can't handle URI because query parameter \"$URL\" is missing"))
52+
context.showErrorPopup(MissingArgumentException("Can't handle URI because query parameter \"$URL\" is missing"))
5253
return
5354
}
5455

@@ -57,50 +58,39 @@ open class CoderProtocolHandler(
5758
authenticate(deploymentURL, queryToken)
5859
} catch (ex: Exception) {
5960
context.logger.error(ex, "Query parameter \"$TOKEN\" is missing from URI $uri")
60-
context.ui.showErrorInfoPopup(
61-
IllegalStateException(
62-
humanizeConnectionError(
63-
deploymentURL.toURL(),
64-
true,
65-
ex
66-
)
67-
)
68-
)
61+
context.showErrorPopup(IllegalStateException(humanizeConnectionError(deploymentURL.toURL(), true, ex)))
6962
return
7063
}
7164

7265
// TODO: Show a dropdown and ask for the workspace if missing. Right now it's not possible because dialogs are quite limited
7366
val workspaceName = params.workspace()
7467
if (workspaceName.isNullOrBlank()) {
7568
context.logger.error("Query parameter \"$WORKSPACE\" is missing from URI $uri")
76-
context.ui.showErrorInfoPopup(MissingArgumentException("Can't handle URI because query parameter \"$WORKSPACE\" is missing"))
69+
context.showErrorPopup(MissingArgumentException("Can't handle URI because query parameter \"$WORKSPACE\" is missing"))
7770
return
7871
}
7972

8073
val workspaces = restClient.workspaces()
8174
val workspace = workspaces.firstOrNull { it.name == workspaceName }
8275
if (workspace == null) {
8376
context.logger.error("There is no workspace with name $workspaceName on $deploymentURL")
84-
context.ui.showErrorInfoPopup(MissingArgumentException("Can't handle URI because workspace with name $workspaceName does not exist"))
77+
context.showErrorPopup(MissingArgumentException("Can't handle URI because workspace with name $workspaceName does not exist"))
8578
return
8679
}
8780

8881
when (workspace.latestBuild.status) {
8982
WorkspaceStatus.PENDING, WorkspaceStatus.STARTING ->
9083
if (restClient.waitForReady(workspace) != true) {
9184
context.logger.error("$workspaceName from $deploymentURL could not be ready on time")
92-
context.ui.showErrorInfoPopup(MissingArgumentException("Can't handle URI because workspace $workspaceName could not be ready on time"))
85+
context.showErrorPopup(MissingArgumentException("Can't handle URI because workspace $workspaceName could not be ready on time"))
9386
return
9487
}
9588

9689
WorkspaceStatus.STOPPING, WorkspaceStatus.STOPPED,
9790
WorkspaceStatus.CANCELING, WorkspaceStatus.CANCELED -> {
9891
if (context.settings.disableAutostart) {
99-
context.ui.showWindow()
100-
context.envPageManager.showPluginEnvironmentsPage(true)
101-
10292
context.logger.warn("$workspaceName from $deploymentURL is not started and autostart is disabled.")
103-
context.ui.showInfoPopup(
93+
context.showInfoPopup(
10494
context.i18n.pnotr("$workspaceName is not running"),
10595
context.i18n.ptrl("Can't handle URI because workspace is not running and autostart is disabled. Please start the workspace manually and execute the URI again."),
10696
context.i18n.ptrl("OK")
@@ -111,14 +101,14 @@ open class CoderProtocolHandler(
111101
restClient.startWorkspace(workspace)
112102
if (restClient.waitForReady(workspace) != true) {
113103
context.logger.error("$workspaceName from $deploymentURL could not be started on time")
114-
context.ui.showErrorInfoPopup(MissingArgumentException("Can't handle URI because workspace $workspaceName could not be started on time"))
104+
context.showErrorPopup(MissingArgumentException("Can't handle URI because workspace $workspaceName could not be started on time"))
115105
return
116106
}
117107
}
118108

119109
WorkspaceStatus.FAILED, WorkspaceStatus.DELETING, WorkspaceStatus.DELETED -> {
120110
context.logger.error("Unable to connect to $workspaceName from $deploymentURL")
121-
context.ui.showErrorInfoPopup(MissingArgumentException("Can't handle URI because because we're unable to connect to workspace $workspaceName"))
111+
context.showErrorPopup(MissingArgumentException("Can't handle URI because because we're unable to connect to workspace $workspaceName"))
122112
return
123113
}
124114

@@ -325,6 +315,25 @@ internal fun getMatchingAgent(
325315
return agent
326316
}
327317

318+
private suspend fun CoderToolboxContext.showErrorPopup(error: Throwable) {
319+
popupPluginMainPage()
320+
this.ui.showErrorInfoPopup(error)
321+
}
322+
323+
private suspend fun CoderToolboxContext.showInfoPopup(
324+
title: LocalizableString,
325+
message: LocalizableString,
326+
okLabel: LocalizableString
327+
) {
328+
popupPluginMainPage()
329+
this.ui.showInfoPopup(title, message, okLabel)
330+
}
331+
332+
private fun CoderToolboxContext.popupPluginMainPage() {
333+
this.ui.showWindow()
334+
this.envPageManager.showPluginEnvironmentsPage(true)
335+
}
336+
328337
/**
329338
* Suspends the coroutine until first true value is received.
330339
*/

0 commit comments

Comments
 (0)