Skip to content

Commit eb5e2ac

Browse files
committed
custom exception class for setup command
1 parent 55870c4 commit eb5e2ac

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

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

+25-11
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,38 @@ class CoderRemoteConnectionHandle {
161161
)
162162
logger.info("Adding ${parameters.ideName} for ${parameters.hostname}:${parameters.projectPath} to recent connections")
163163
recentConnectionsService.addRecentConnection(parameters.toRecentWorkspaceConnection())
164+
} catch (e: CoderSetupCommandException) {
165+
logger.error("Failed to run setup command", e)
166+
showConnectionErrorMessage(
167+
e.message ?: "Unknown error",
168+
"gateway.connector.coder.setup-command.failed",
169+
)
164170
} catch (e: Exception) {
165171
if (isCancellation(e)) {
166172
logger.info("Connection canceled due to ${e.javaClass.simpleName}")
167173
} else {
168174
logger.error("Failed to connect (will not retry)", e)
169-
// The dialog will close once we return so write the error
170-
// out into a new dialog.
171-
ApplicationManager.getApplication().invokeAndWait {
172-
Messages.showMessageDialog(
173-
e.message ?: e.javaClass.simpleName ?: "Aborted",
174-
CoderGatewayBundle.message("gateway.connector.coder.connection.failed"),
175-
Messages.getErrorIcon(),
176-
)
177-
}
175+
showConnectionErrorMessage(
176+
e.message ?: e.javaClass.simpleName ?: "Aborted",
177+
"gateway.connector.coder.connection.failed"
178+
)
178179
}
179180
}
180181
}
181182
}
182183

184+
// The dialog will close once we return so write the error
185+
// out into a new dialog.
186+
private fun showConnectionErrorMessage(message: String, titleKey: String) {
187+
ApplicationManager.getApplication().invokeAndWait {
188+
Messages.showMessageDialog(
189+
message,
190+
CoderGatewayBundle.message(titleKey),
191+
Messages.getErrorIcon(),
192+
)
193+
}
194+
}
195+
183196
/**
184197
* Return a new (non-EAP) IDE if we should update.
185198
*/
@@ -521,6 +534,7 @@ class CoderRemoteConnectionHandle {
521534

522535
companion object {
523536
val logger = Logger.getInstance(CoderRemoteConnectionHandle::class.java.simpleName)
537+
@Throws(CoderSetupCommandException::class)
524538
fun processSetupCommand(
525539
ignoreSetupFailure: Boolean,
526540
execCommand: () -> String
@@ -533,11 +547,11 @@ class CoderRemoteConnectionHandle {
533547
?.let { it.substring(it.indexOf(GATEWAY_SETUP_COMMAND_ERROR) + GATEWAY_SETUP_COMMAND_ERROR.length).trim() }
534548

535549
if (!errorText.isNullOrBlank()) {
536-
throw Exception(errorText)
550+
throw CoderSetupCommandException(errorText)
537551
}
538552
} catch (ex: Exception) {
539553
if (!ignoreSetupFailure) {
540-
throw ex
554+
throw CoderSetupCommandException(ex.message ?: "Unknown error", ex)
541555
}
542556
}
543557
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.coder.gateway
2+
3+
class CoderSetupCommandException : Exception {
4+
5+
constructor(message: String) : super(message)
6+
constructor(message: String, cause: Throwable) : super(message, cause)
7+
}

src/main/resources/messages/CoderGatewayBundle.properties

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ gateway.connector.coder.connection.provider.title=Connecting to Coder workspace.
4949
gateway.connector.coder.connecting=Connecting...
5050
gateway.connector.coder.connecting.retry=Connecting (attempt {0})...
5151
gateway.connector.coder.connection.failed=Failed to connect
52+
gateway.connector.coder.setup-command.failed=Failed to set up
5253
gateway.connector.coder.connecting.failed.retry=Failed to connect...retrying {0}
5354
gateway.connector.settings.data-directory.title=Data directory
5455
gateway.connector.settings.data-directory.comment=Directories are created \

src/test/kotlin/com/coder/gateway/util/SetupCommandTest.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.coder.gateway.util
22

33
import com.coder.gateway.CoderRemoteConnectionHandle.Companion.processSetupCommand
4+
import com.coder.gateway.CoderSetupCommandException
45
import org.junit.jupiter.api.Test
56
import org.junit.jupiter.api.assertThrows
67
import kotlin.test.assertEquals
@@ -11,7 +12,7 @@ internal class SetupCommandTest {
1112
fun executionErrors() {
1213
assertEquals(
1314
"Execution error",
14-
assertThrows<Exception> {
15+
assertThrows<CoderSetupCommandException> {
1516
processSetupCommand(false) { throw Exception("Execution error") }
1617
}.message
1718
)
@@ -22,7 +23,7 @@ internal class SetupCommandTest {
2223
fun setupScriptError() {
2324
assertEquals(
2425
"Your IDE is expired, please update",
25-
assertThrows<Exception> {
26+
assertThrows<CoderSetupCommandException> {
2627
processSetupCommand(false) {
2728
"""
2829
execution line 1

0 commit comments

Comments
 (0)