diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandExecuteResult.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandExecuteResult.kt index 9076951b4..3ec14fe62 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandExecuteResult.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandExecuteResult.kt @@ -58,7 +58,7 @@ public sealed class CommandExecuteResult { /** 执行执行时发生了一个非法参数错误 */ public class IllegalArgument( /** 指令执行时发生的错误 */ - public override val exception: IllegalArgumentException, + public override val exception: IllegalCommandArgumentException, /** 尝试执行的指令 */ public override val command: Command, /** 尝试执行的指令名 */ @@ -180,7 +180,7 @@ public fun CommandExecuteResult.isExecutionException(): Boolean { } /** - * 当 [this] 为 [CommandExecuteResult.ExecutionFailed] 时返回 `true` + * 当 [this] 为 [CommandExecuteResult.PermissionDenied] 时返回 `true` */ @JvmSynthetic public fun CommandExecuteResult.isPermissionDenied(): Boolean { @@ -192,7 +192,7 @@ public fun CommandExecuteResult.isPermissionDenied(): Boolean { } /** - * 当 [this] 为 [CommandExecuteResult.ExecutionFailed] 时返回 `true` + * 当 [this] 为 [CommandExecuteResult.CommandNotFound] 时返回 `true` */ @JvmSynthetic public fun CommandExecuteResult.isCommandNotFound(): Boolean { @@ -204,7 +204,7 @@ public fun CommandExecuteResult.isCommandNotFound(): Boolean { } /** - * 当 [this] 为 [CommandExecuteResult.ExecutionFailed] 或 [CommandExecuteResult.CommandNotFound] 时返回 `true` + * 当 [this] 为 [CommandExecuteResult.ExecutionFailed], [CommandExecuteResult.IllegalArgument] 或 [CommandExecuteResult.CommandNotFound] 时返回 `true` */ @JvmSynthetic public fun CommandExecuteResult.isFailure(): Boolean { diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandSender.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandSender.kt index 219c2485e..0f8314384 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandSender.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandSender.kt @@ -282,6 +282,13 @@ public sealed class AbstractCommandSender : CommandSender, CoroutineScope { if (this is CommandSenderOnMessage<*>) { val cause = e.rootCauseOrSelf + // TODO: 2020/10/17 + // CommandArgumentParserException 作为 IllegalCommandArgumentException 不会再进入此函数 + // 已在 + // - [console] CommandManagerImpl.commandListener + // - [terminal] ConsoleThread.kt + // 处理 + val message = cause .takeIf { it is CommandArgumentParserException }?.message ?: "${cause::class.simpleName.orEmpty()}: ${cause.message}" diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/IllegalCommandArgumentException.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/IllegalCommandArgumentException.kt new file mode 100644 index 000000000..0e8936524 --- /dev/null +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/IllegalCommandArgumentException.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2019-2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found via the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + * + */ + +@file:Suppress("unused") + +package net.mamoe.mirai.console.command + +import net.mamoe.mirai.console.command.descriptor.CommandArgumentParserException + +/** + * 在处理参数时遇到的 _正常_ 错误. 如参数不符合规范, 参数值越界等. + * + * [message] 将会发送给指令调用方. + * + * @see CommandArgumentParserException + */ +public open class IllegalCommandArgumentException : IllegalArgumentException { + public constructor() : super() + public constructor(message: String?) : super(message) + public constructor(message: String?, cause: Throwable?) : super(message, cause) + public constructor(cause: Throwable?) : super(cause) +} diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandArgumentParserException.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandArgumentParserException.kt index faae4cf1e..25a59067d 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandArgumentParserException.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/descriptor/CommandArgumentParserException.kt @@ -11,15 +11,18 @@ package net.mamoe.mirai.console.command.descriptor +import net.mamoe.mirai.console.command.IllegalCommandArgumentException + /** * 在解析参数时遇到的 _正常_ 错误. 如参数不符合规范等. * * [message] 将会发送给指令调用方. * + * @see IllegalCommandArgumentException * @see CommandValueArgumentParser * @see CommandValueArgumentParser.illegalArgument */ -public class CommandArgumentParserException : RuntimeException { +public class CommandArgumentParserException : IllegalCommandArgumentException { public constructor() : super() public constructor(message: String?) : super(message) public constructor(message: String?, cause: Throwable?) : super(message, cause) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/executeCommandInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/executeCommandInternal.kt index a9ff54cca..cfd1648d7 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/executeCommandInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/executeCommandInternal.kt @@ -37,7 +37,7 @@ internal suspend fun CommandSender.executeCommandInternal( ) }, onFailure = { exception -> - return if (exception is IllegalArgumentException) CommandExecuteResult.IllegalArgument( + return if (exception is IllegalCommandArgumentException) CommandExecuteResult.IllegalArgument( commandName = commandName, command = command, exception = exception,