Skip to content

Commit 88134ba

Browse files
committed
setup script can communicate an error message to the end user
1 parent cdc6fda commit 88134ba

File tree

3 files changed

+74
-7
lines changed

3 files changed

+74
-7
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ package com.coder.gateway
33
object CoderGatewayConstants {
44
const val GATEWAY_CONNECTOR_ID = "Coder.Gateway.Connector"
55
const val GATEWAY_RECENT_CONNECTIONS_ID = "Coder.Gateway.Recent.Connections"
6+
const val GATEWAY_SETUP_COMMAND_ERROR = "CODER_SETUP_ERROR"
67
}

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

+26-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package com.coder.gateway
44

5+
import com.coder.gateway.CoderGatewayConstants.GATEWAY_SETUP_COMMAND_ERROR
56
import com.coder.gateway.cli.CoderCLIManager
67
import com.coder.gateway.models.WorkspaceProjectIDE
78
import com.coder.gateway.models.toIdeWithStatus
@@ -412,18 +413,16 @@ class CoderRemoteConnectionHandle {
412413
) {
413414
if (setupCommand.isNotBlank()) {
414415
indicator.text = "Running setup command..."
415-
try {
416-
exec(workspace, setupCommand)
417-
} catch (ex: Exception) {
418-
if (!ignoreSetupFailure) {
419-
throw ex
420-
}
421-
}
416+
processSetupCommand(
417+
{ exec(workspace, setupCommand) },
418+
ignoreSetupFailure
419+
)
422420
} else {
423421
logger.info("No setup command to run on ${workspace.hostname}")
424422
}
425423
}
426424

425+
427426
/**
428427
* Execute a command in the IDE's bin directory.
429428
* This exists since the accessor does not provide a generic exec.
@@ -523,5 +522,25 @@ class CoderRemoteConnectionHandle {
523522

524523
companion object {
525524
val logger = Logger.getInstance(CoderRemoteConnectionHandle::class.java.simpleName)
525+
fun processSetupCommand(
526+
output: () -> String,
527+
ignoreSetupFailure: Boolean
528+
) {
529+
try {
530+
val errorText = output
531+
.invoke()
532+
.lines()
533+
.firstOrNull { it.contains(GATEWAY_SETUP_COMMAND_ERROR) }
534+
?.let { it.substring(it.indexOf(GATEWAY_SETUP_COMMAND_ERROR) + GATEWAY_SETUP_COMMAND_ERROR.length).trim() }
535+
536+
if (!errorText.isNullOrBlank()) {
537+
throw Exception(errorText)
538+
}
539+
} catch (ex: Exception) {
540+
if (!ignoreSetupFailure) {
541+
throw ex
542+
}
543+
}
544+
}
526545
}
527546
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.coder.gateway.util
2+
3+
import com.coder.gateway.CoderRemoteConnectionHandle.Companion.processSetupCommand
4+
import org.junit.jupiter.api.Test
5+
import org.junit.jupiter.api.assertThrows
6+
import kotlin.test.assertEquals
7+
8+
internal class SetupCommandTest {
9+
10+
@Test
11+
fun executionErrors() {
12+
assertEquals(
13+
"Execution error",
14+
assertThrows<Exception> {
15+
processSetupCommand({ throw Exception("Execution error") }, false)
16+
}.message
17+
)
18+
processSetupCommand({ throw Exception("Execution error") }, true)
19+
}
20+
21+
@Test
22+
fun setupScriptError() {
23+
assertEquals(
24+
"Your IDE is expired, please update",
25+
assertThrows<Exception> {
26+
processSetupCommand({
27+
"""
28+
execution line 1
29+
execution line 2
30+
CODER_SETUP_ERRORYour IDE is expired, please update
31+
execution line 3
32+
"""
33+
}, false)
34+
}.message
35+
)
36+
37+
processSetupCommand({
38+
"""
39+
execution line 1
40+
execution line 2
41+
CODER_SETUP_ERRORYour IDE is expired, please update
42+
execution line 3
43+
"""
44+
}, true)
45+
46+
}
47+
}

0 commit comments

Comments
 (0)