Skip to content

Commit b3e20f8

Browse files
committed
Allow canceling connection
Also, getting an error about project being null so just remove it. It is now an overload where the project is not specified at all. Previously it was an optional parameter, so it should still work in all versions.
1 parent a30bf4f commit b3e20f8

File tree

3 files changed

+57
-50
lines changed

3 files changed

+57
-50
lines changed

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

+1-1
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, project = null) {
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

+55-49
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
@@ -423,12 +424,7 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
423424
tableOfWorkspaces.listTableModel.items = emptyList()
424425

425426
// Authenticate and load in a background process with progress.
426-
// 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-
) {
427+
return LifetimeDefinition().launchUnderBackgroundProgress(CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.cli.downloader.dialog.title")) {
432428
try {
433429
this.indicator.text = "Authenticating client..."
434430
authenticate(deploymentURL, token.first)
@@ -456,51 +452,61 @@ class CoderWorkspacesStepView(val setNextButtonEnabled: (Boolean) -> Unit) : Cod
456452
tableOfWorkspaces.setEmptyState(CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.connect.text.connected", deploymentURL.host))
457453
tfUrlComment?.text = CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.connect.text.connected", deploymentURL.host)
458454
} 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-
)
455+
if (isCancellation(e)) {
456+
tfUrlComment?.text = CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.connect.text.comment",
457+
CoderGatewayBundle.message("gateway.connector.view.coder.workspaces.connect.text"))
458+
tableOfWorkspaces.setEmptyState(CoderGatewayBundle.message(
459+
"gateway.connector.view.workspaces.connect.canceled",
460+
deploymentURL.host,
461+
))
462+
logger.info("Connection canceled due to ${e.javaClass.simpleName}")
463+
} else {
464+
val reason = e.message ?: CoderGatewayBundle.message("gateway.connector.view.workspaces.connect.no-reason")
465+
val msg = when (e) {
466+
is java.nio.file.AccessDeniedException -> CoderGatewayBundle.message("gateway.connector.view.workspaces.connect.access-denied", e.file)
467+
is UnknownHostException -> CoderGatewayBundle.message("gateway.connector.view.workspaces.connect.unknown-host", e.message ?: deploymentURL.host)
468+
is InvalidExitValueException -> CoderGatewayBundle.message("gateway.connector.view.workspaces.connect.unexpected-exit", e.exitValue)
469+
is AuthenticationResponseException -> {
470+
CoderGatewayBundle.message(
471+
"gateway.connector.view.workspaces.connect.unauthorized",
472+
deploymentURL,
473+
)
474+
}
475+
is SocketTimeoutException -> {
476+
CoderGatewayBundle.message(
477+
"gateway.connector.view.workspaces.connect.timeout",
478+
deploymentURL,
479+
)
480+
}
481+
is ResponseException, is ConnectException -> {
482+
CoderGatewayBundle.message(
483+
"gateway.connector.view.workspaces.connect.download-failed",
484+
reason,
485+
)
486+
}
487+
is SSLHandshakeException -> {
488+
CoderGatewayBundle.message(
489+
"gateway.connector.view.workspaces.connect.ssl-error",
490+
deploymentURL.host,
491+
reason,
492+
)
493+
}
494+
else -> reason
481495
}
482-
is SSLHandshakeException -> {
483-
CoderGatewayBundle.message(
484-
"gateway.connector.view.workspaces.connect.ssl-error",
485-
deploymentURL.host,
486-
reason,
487-
)
496+
// It would be nice to place messages directly into the table
497+
// but it does not support wrapping or markup so place it in the
498+
// comment field of the URL input instead.
499+
tfUrlComment?.foreground = UIUtil.getErrorForeground()
500+
tfUrlComment?.text = msg
501+
tableOfWorkspaces.setEmptyState(CoderGatewayBundle.message(
502+
"gateway.connector.view.workspaces.connect.failed",
503+
deploymentURL.host,
504+
))
505+
logger.error(msg, e)
506+
507+
if (e is AuthenticationResponseException) {
508+
cs.launch { onAuthFailure?.invoke() }
488509
}
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() }
504510
}
505511
}
506512
}

src/main/resources/messages/CoderGatewayBundle.properties

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ gateway.connector.view.coder.workspaces.unsupported.os.info=Gateway supports onl
2626
gateway.connector.view.coder.workspaces.invalid.coder.version=Could not parse Coder version {0}. Coder Gateway plugin might not be compatible with this version. <a href='https://coder.com/docs/v2/latest/ides/gateway#creating-a-new-jetbrains-gateway-connection'>Connect to a Coder workspace manually</a>
2727
gateway.connector.view.coder.workspaces.unsupported.coder.version=Coder version {0} might not be compatible with this plugin version. <a href='https://coder.com/docs/v2/latest/ides/gateway#creating-a-new-jetbrains-gateway-connection'>Connect to a Coder workspace manually</a>
2828
gateway.connector.view.workspaces.connect.failed=Connection to {0} failed. See above for details.
29+
gateway.connector.view.workspaces.connect.canceled=Connection to {0} canceled.
2930
gateway.connector.view.workspaces.connect.no-reason=No reason was provided.
3031
gateway.connector.view.workspaces.connect.access-denied=Access denied to {0}.
3132
gateway.connector.view.workspaces.connect.unknown-host=Unknown host {0}.

0 commit comments

Comments
 (0)