refactor(partnerctl-exception): add command interrupted exception, and unify exception handling

This commit is contained in:
2026-05-03 20:08:18 +08:00
parent 78877f14d5
commit 7fcdb1c671
2 changed files with 34 additions and 2 deletions

View File

@@ -3,6 +3,8 @@ package work.slhaf.partner.ctl
import picocli.AutoComplete import picocli.AutoComplete
import picocli.CommandLine import picocli.CommandLine
import work.slhaf.partner.ctl.commands.* import work.slhaf.partner.ctl.commands.*
import work.slhaf.partner.ctl.support.CommandInterrupted
import work.slhaf.partner.ctl.ui.PromptCancelledException
import kotlin.system.exitProcess import kotlin.system.exitProcess
@CommandLine.Command( @CommandLine.Command(
@@ -28,6 +30,30 @@ class PartnerCtl : Runnable {
} }
fun main(args: Array<String>) { fun main(args: Array<String>) {
val exitCode = CommandLine(PartnerCtl()).execute(*args) val commandLine = CommandLine(PartnerCtl())
exitProcess(exitCode) commandLine.executionExceptionHandler = CommandLine.IExecutionExceptionHandler { ex, _, _ ->
return@IExecutionExceptionHandler when (ex) {
is PromptCancelledException -> {
println()
println("[warn] Cancelled")
130
}
is CommandInterrupted -> {
println()
println("[error] ${ex.message}")
ex.exitCode
}
else -> {
System.err.println("[error] ${ex.message ?: "Unexpected error"}")
if (System.getenv("PARTNERCTL_DEBUG") == "1") {
ex.printStackTrace(System.err)
}
1
}
}
}
exitProcess(commandLine.execute(*args))
} }

View File

@@ -0,0 +1,6 @@
package work.slhaf.partner.ctl.support
class CommandInterrupted(
override val message: String,
val exitCode: Int = 1,
) : RuntimeException(message)