Stop input service when catch any error

Fix #229
This commit is contained in:
Karlatemp 2020-11-29 10:03:24 +08:00
parent e391936679
commit e5bc588da7
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8

View File

@ -55,8 +55,8 @@ internal fun startupConsoleThread() {
} }
MiraiConsole.launch(CoroutineName("Console Command")) { MiraiConsole.launch(CoroutineName("Console Command")) {
while (true) { while (true) {
try { val next = try {
val next = MiraiConsole.requestInput("").let { MiraiConsole.requestInput("").let {
when { when {
it.isBlank() -> it it.isBlank() -> it
it.startsWith(CommandManager.commandPrefix) -> it it.startsWith(CommandManager.commandPrefix) -> it
@ -64,9 +64,25 @@ internal fun startupConsoleThread() {
else -> CommandManager.commandPrefix + it else -> CommandManager.commandPrefix + it
} }
} }
if (next.isBlank()) { } catch (e: InterruptedException) {
continue return@launch
} } catch (e: CancellationException) {
return@launch
} catch (e: UserInterruptException) {
BuiltInCommands.StopCommand.run { ConsoleCommandSender.handle() }
return@launch
} catch (eof: EndOfFileException) {
consoleLogger.warning("Closing input service...")
return@launch
} catch (e: Throwable) {
consoleLogger.error("Error in reading next command", e)
consoleLogger.warning("Closing input service...")
return@launch
}
if (next.isBlank()) {
continue
}
try {
// consoleLogger.debug("INPUT> $next") // consoleLogger.debug("INPUT> $next")
when (val result = ConsoleCommandSender.executeCommand(next)) { when (val result = ConsoleCommandSender.executeCommand(next)) {
is Success -> { is Success -> {
@ -97,12 +113,6 @@ internal fun startupConsoleThread() {
return@launch return@launch
} catch (e: CancellationException) { } catch (e: CancellationException) {
return@launch return@launch
} catch (e: UserInterruptException) {
BuiltInCommands.StopCommand.run { ConsoleCommandSender.handle() }
return@launch
} catch (eof: EndOfFileException) {
consoleLogger.warning("Closing input service...")
return@launch
} catch (e: Throwable) { } catch (e: Throwable) {
consoleLogger.error("Unhandled exception", e) consoleLogger.error("Unhandled exception", e)
} }