IllegalCommandArgumentException

This commit is contained in:
Karlatemp 2020-10-17 11:47:40 +08:00
parent 82de404149
commit d4c147a8a9
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
5 changed files with 45 additions and 6 deletions

View File

@ -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 {

View File

@ -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}"

View File

@ -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)
}

View File

@ -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)

View File

@ -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,