mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(partnerctl-exception): add command interrupted exception, and unify exception handling
This commit is contained in:
@@ -3,6 +3,8 @@ package work.slhaf.partner.ctl
|
||||
import picocli.AutoComplete
|
||||
import picocli.CommandLine
|
||||
import work.slhaf.partner.ctl.commands.*
|
||||
import work.slhaf.partner.ctl.support.CommandInterrupted
|
||||
import work.slhaf.partner.ctl.ui.PromptCancelledException
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
@CommandLine.Command(
|
||||
@@ -28,6 +30,30 @@ class PartnerCtl : Runnable {
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val exitCode = CommandLine(PartnerCtl()).execute(*args)
|
||||
exitProcess(exitCode)
|
||||
val commandLine = CommandLine(PartnerCtl())
|
||||
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))
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package work.slhaf.partner.ctl.support
|
||||
|
||||
class CommandInterrupted(
|
||||
override val message: String,
|
||||
val exitCode: Int = 1,
|
||||
) : RuntimeException(message)
|
||||
Reference in New Issue
Block a user