Skip to content

Commit e1ba927

Browse files
committed
fix: exit handling for invalid manifest
Signed-off-by: Sam Gammon <[email protected]>
1 parent 20f8e04 commit e1ba927

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

packages/cli/src/main/kotlin/elide/tool/cli/AbstractToolCommand.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,26 +181,27 @@ abstract class AbstractToolCommand<Context>:
181181
if (err != null && HandledExit.isHandledExit(err)) {
182182
throw err // re-throw for outer
183183
}
184-
val (emitStackAndLog, rethrow) = when (err) {
185-
null -> true to true
184+
val (emitStackAndLog, rethrow, subject) = when (err) {
185+
null -> Triple(true, true, err)
186186
is PackageManifestService.ManifestInvalid -> {
187187
if (Statics.noColor) {
188-
logging.error(err.message)
188+
Statics.terminal.println(err.message)
189189
} else {
190-
logging.error(TextColors.red(err.message ?: "Project manifest error"))
190+
Statics.terminal.println(TextColors.red(err.message ?: "Project manifest error"))
191191
}
192192

193-
false to false
193+
// exit with code 3; do not provide a cause message otherwise it will be logged
194+
Triple(false, false, HandledExit.create(3))
194195
}
195-
else -> true to true
196+
else -> Triple(true, true, err)
196197
}
197198
if (emitStackAndLog) {
198199
val stack = err?.stackTrace?.joinToString("\n") ?: "(unknown)"
199200
val label = err ?: "(unknown)"
200201
logging.error("Uncaught fatal exception: $label\n$stack")
201202
}
202203
exit.set(1)
203-
if (err != null && rethrow) throw err
204+
if (err != null && rethrow) throw (subject ?: err)
204205
}
205206
}
206207
}

packages/cli/src/main/kotlin/elide/tool/cli/cmd/repl/HandledExit.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ internal class HandledExit private constructor (
2727
}
2828

2929
@JvmStatic fun notify(exitCode: Int, message: String? = null, cause: Throwable? = null): Nothing {
30-
throw HandledExit(
30+
throw create(
31+
exitCode = exitCode,
32+
message = message,
33+
cause = cause,
34+
)
35+
}
36+
37+
@JvmStatic fun create(exitCode: Int, message: String? = null, cause: Throwable? = null): HandledExit {
38+
return HandledExit(
3139
exitCode = exitCode,
3240
causeMessage = message,
3341
cause = cause,

packages/cli/src/main/kotlin/elide/tool/cli/main.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import elide.runtime.gvm.kotlin.KotlinLanguage
3333
import elide.tool.cli.Elide.Companion.installStatics
3434
import elide.tool.cli.cmd.repl.HandledExit
3535
import elide.tooling.cli.Statics
36+
import elide.tooling.project.PackageManifestService
3637

3738
// Whether to enable the experimental V2 entrypoint through Clikt.
3839
private const val ENABLE_CLI_ENTRY_V2 = false
@@ -109,12 +110,12 @@ private inline fun runInner(args: Array<String>): Int = when (ENABLE_CLI_ENTRY_V
109110
} catch (_: PrintHelpMessage) {
110111
try {
111112
sorryIHaveToFactory(args).usage(System.out)
113+
0
112114
} catch (err: Throwable) {
113115
println("Failed to print help message: ${err.message}")
114116
err.printStackTrace()
115117
exitProcess(1)
116118
}
117-
0
118119
} catch (err: RuntimeException) {
119120
println("Uncaught error while running command: ${err.message}")
120121
err.printStackTrace()

0 commit comments

Comments
 (0)