Skip to content

Commit 3108f7d

Browse files
committed
impl: resilience when url query parameter is missing
- a pop-up dialog is displayed asking for the deployment URL - an error dialog is displayed if the URL is still not provided by the user
1 parent 5b7f3e9 commit 3108f7d

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

src/main/kotlin/com/coder/toolbox/CoderRemoteProvider.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import com.coder.toolbox.settings.CoderSettings
99
import com.coder.toolbox.settings.Source
1010
import com.coder.toolbox.util.CoderProtocolHandler
1111
import com.coder.toolbox.util.DialogUi
12-
import com.coder.toolbox.util.toQueryParameters
1312
import com.coder.toolbox.views.Action
1413
import com.coder.toolbox.views.CoderSettingsPage
1514
import com.coder.toolbox.views.ConnectPage
@@ -233,8 +232,7 @@ class CoderRemoteProvider(
233232
* Handle incoming links (like from the dashboard).
234233
*/
235234
override suspend fun handleUri(uri: URI) {
236-
val params = uri.toQueryParameters()
237-
linkHandler.handle(params) { restClient, cli ->
235+
linkHandler.handle(uri) { restClient, cli ->
238236
// stop polling and de-initialize resources
239237
close()
240238
// start initialization with the new settings

src/main/kotlin/com/coder/toolbox/util/CoderProtocolHandler.kt

+20-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import kotlinx.coroutines.launch
1818
import kotlinx.coroutines.yield
1919
import okhttp3.OkHttpClient
2020
import java.net.HttpURLConnection
21+
import java.net.URI
2122
import java.net.URL
2223

2324
open class CoderProtocolHandler(
@@ -35,19 +36,19 @@ open class CoderProtocolHandler(
3536
* connectable state.
3637
*/
3738
suspend fun handle(
38-
parameters: Map<String, String>,
39+
uri: URI,
3940
onReinitialize: suspend (CoderRestClient, CoderCLIManager) -> Unit
4041
) {
41-
val deploymentURL =
42-
parameters.url() ?: dialogUi.ask(
43-
context.i18n.ptrl("Deployment URL"),
44-
context.i18n.ptrl("Enter the full URL of your Coder deployment")
45-
)
42+
val params = uri.toQueryParameters()
43+
44+
val deploymentURL = params.url() ?: askUrl()
4645
if (deploymentURL.isNullOrBlank()) {
47-
throw MissingArgumentException("Query parameter \"$URL\" is missing")
46+
context.logger.error("Query parameter \"$URL\" is missing from URI $uri")
47+
context.ui.showErrorInfoPopup(MissingArgumentException("Can't handle URI because query parameter \"$URL\" is missing"))
48+
return
4849
}
4950

50-
val queryTokenRaw = parameters.token()
51+
val queryTokenRaw = params.token()
5152
val queryToken = if (!queryTokenRaw.isNullOrBlank()) {
5253
Pair(queryTokenRaw, Source.QUERY)
5354
} else {
@@ -61,7 +62,7 @@ open class CoderProtocolHandler(
6162

6263
// TODO: Show a dropdown and ask for the workspace if missing.
6364
val workspaceName =
64-
parameters.workspace() ?: throw MissingArgumentException("Query parameter \"$WORKSPACE\" is missing")
65+
params.workspace() ?: throw MissingArgumentException("Query parameter \"$WORKSPACE\" is missing")
6566

6667
val workspaces = restClient.workspaces()
6768
val workspace =
@@ -99,7 +100,7 @@ open class CoderProtocolHandler(
99100
}
100101

101102
// TODO: Show a dropdown and ask for an agent if missing.
102-
val agent = getMatchingAgent(parameters, workspace)
103+
val agent = getMatchingAgent(params, workspace)
103104
val status = WorkspaceAndAgentStatus.from(workspace, agent)
104105

105106
if (status.pending()) {
@@ -147,6 +148,15 @@ open class CoderProtocolHandler(
147148
}
148149
}
149150

151+
private suspend fun askUrl(): String? {
152+
context.ui.showWindow()
153+
context.envPageManager.showPluginEnvironmentsPage(false)
154+
return dialogUi.ask(
155+
context.i18n.ptrl("Deployment URL"),
156+
context.i18n.ptrl("Enter the full URL of your Coder deployment")
157+
)
158+
}
159+
150160
/**
151161
* Return an authenticated Coder CLI, asking for the token as long as it
152162
* continues to result in an authentication failure and token authentication

0 commit comments

Comments
 (0)