Skip to content

Commit 528836f

Browse files
committed
Allow canceling connection
1 parent 2776385 commit 528836f

File tree

2 files changed

+50
-49
lines changed

2 files changed

+50
-49
lines changed

src/main/kotlin/com/coder/gateway/CoderRemoteConnectionHandle.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class CoderRemoteConnectionHandle {
5050

5151
suspend fun connect(getParameters: (indicator: ProgressIndicator) -> Map<String, String>) {
5252
val clientLifetime = LifetimeDefinition()
53-
clientLifetime.launchUnderBackgroundProgress(CoderGatewayBundle.message("gateway.connector.coder.connection.provider.title"), canBeCancelled = true, isIndeterminate = true) {
53+
clientLifetime.launchUnderBackgroundProgress(CoderGatewayBundle.message("gateway.connector.coder.connection.provider.title")) {
5454
try {
5555
val parameters = getParameters(indicator)
5656
logger.debug("Creating connection handle", parameters)

src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.coder.gateway.sdk.TemplateIconDownloader
1818
import com.coder.gateway.sdk.ex.AuthenticationResponseException
1919
import com.coder.gateway.sdk.ex.TemplateResponseException
2020
import com.coder.gateway.sdk.ex.WorkspaceResponseException
21+
import com.coder.gateway.sdk.isCancellation
2122
import com.coder.gateway.sdk.toURL
2223
import com.coder.gateway.sdk.v2.models.WorkspaceStatus
2324
import com.coder.gateway.sdk.v2.models.toAgentModels
@@ -424,11 +425,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
424425

425426
// Authenticate and load in a background process with progress.
426427
// TODO: Make this cancelable.
427-
return LifetimeDefinition().launchUnderBackgroundProgress(
428-
CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.cli.downloader.dialog.title"),
429-
canBeCancelled = false,
430-
isIndeterminate = true
431-
) {
428+
return LifetimeDefinition().launchUnderBackgroundProgress(CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.cli.downloader.dialog.title")) {
432429
try {
433430
this.indicator.text = "Authenticating client..."
434431
authenticate(deploymentURL, token.first)
@@ -456,51 +453,55 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
456453
tableOfWorkspaces.setEmptyState(CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.connect.text.connected", deploymentURL.host))
457454
tfUrlComment?.text = CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.connect.text.connected", deploymentURL.host)
458455
} catch (e: Exception) {
459-
val reason = e.message ?: CoderGatewayBundle.message("gateway.connector.view.workspaces.connect.no-reason")
460-
val msg = when (e) {
461-
is java.nio.file.AccessDeniedException -> CoderGatewayBundle.message("gateway.connector.view.workspaces.connect.access-denied", e.file)
462-
is UnknownHostException -> CoderGatewayBundle.message("gateway.connector.view.workspaces.connect.unknown-host", e.message ?: deploymentURL.host)
463-
is InvalidExitValueException -> CoderGatewayBundle.message("gateway.connector.view.workspaces.connect.unexpected-exit", e.exitValue)
464-
is AuthenticationResponseException -> {
465-
CoderGatewayBundle.message(
466-
"gateway.connector.view.workspaces.connect.unauthorized",
467-
deploymentURL,
468-
)
469-
}
470-
is SocketTimeoutException -> {
471-
CoderGatewayBundle.message(
472-
"gateway.connector.view.workspaces.connect.timeout",
473-
deploymentURL,
474-
)
475-
}
476-
is ResponseException, is ConnectException -> {
477-
CoderGatewayBundle.message(
478-
"gateway.connector.view.workspaces.connect.download-failed",
479-
reason,
480-
)
456+
if (isCancellation(e)) {
457+
CoderRemoteConnectionHandle.logger.info("Connection canceled due to ${e.javaClass.simpleName}")
458+
} else {
459+
val reason = e.message ?: CoderGatewayBundle.message("gateway.connector.view.workspaces.connect.no-reason")
460+
val msg = when (e) {
461+
is java.nio.file.AccessDeniedException -> CoderGatewayBundle.message("gateway.connector.view.workspaces.connect.access-denied", e.file)
462+
is UnknownHostException -> CoderGatewayBundle.message("gateway.connector.view.workspaces.connect.unknown-host", e.message ?: deploymentURL.host)
463+
is InvalidExitValueException -> CoderGatewayBundle.message("gateway.connector.view.workspaces.connect.unexpected-exit", e.exitValue)
464+
is AuthenticationResponseException -> {
465+
CoderGatewayBundle.message(
466+
"gateway.connector.view.workspaces.connect.unauthorized",
467+
deploymentURL,
468+
)
469+
}
470+
is SocketTimeoutException -> {
471+
CoderGatewayBundle.message(
472+
"gateway.connector.view.workspaces.connect.timeout",
473+
deploymentURL,
474+
)
475+
}
476+
is ResponseException, is ConnectException -> {
477+
CoderGatewayBundle.message(
478+
"gateway.connector.view.workspaces.connect.download-failed",
479+
reason,
480+
)
481+
}
482+
is SSLHandshakeException -> {
483+
CoderGatewayBundle.message(
484+
"gateway.connector.view.workspaces.connect.ssl-error",
485+
deploymentURL.host,
486+
reason,
487+
)
488+
}
489+
else -> reason
481490
}
482-
is SSLHandshakeException -> {
483-
CoderGatewayBundle.message(
484-
"gateway.connector.view.workspaces.connect.ssl-error",
485-
deploymentURL.host,
486-
reason,
487-
)
491+
// It would be nice to place messages directly into the table
492+
// but it does not support wrapping or markup so place it in the
493+
// comment field of the URL input instead.
494+
tfUrlComment?.foreground = UIUtil.getErrorForeground()
495+
tfUrlComment?.text = msg
496+
tableOfWorkspaces.setEmptyState(CoderGatewayBundle.message(
497+
"gateway.connector.view.workspaces.connect.failed",
498+
deploymentURL.host,
499+
))
500+
logger.error(msg, e)
501+
502+
if (e is AuthenticationResponseException) {
503+
cs.launch { onAuthFailure?.invoke() }
488504
}
489-
else -> reason
490-
}
491-
// It would be nice to place messages directly into the table
492-
// but it does not support wrapping or markup so place it in the
493-
// comment field of the URL input instead.
494-
tfUrlComment?.foreground = UIUtil.getErrorForeground()
495-
tfUrlComment?.text = msg
496-
tableOfWorkspaces.setEmptyState(CoderGatewayBundle.message(
497-
"gateway.connector.view.workspaces.connect.failed",
498-
deploymentURL.host,
499-
))
500-
logger.error(msg, e)
501-
502-
if (e is AuthenticationResponseException) {
503-
cs.launch { onAuthFailure?.invoke() }
504505
}
505506
}
506507
}

0 commit comments

Comments
 (0)