Added CommandExecutionException which wraps any exception thrown during the execution of command handlers. Should be handled using CommandManager#registerExceptionHandler, similar to NoSuchCommandException, ArgumentParseException, etc.

This commit is contained in:
jmp 2020-11-19 19:50:17 -08:00 committed by Alexander Söderberg
parent 2f0ded5be6
commit 7df6917fe4
14 changed files with 240 additions and 32 deletions

View file

@ -26,6 +26,7 @@ package cloud.commandframework.javacord;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.StaticArgument;
import cloud.commandframework.exceptions.ArgumentParseException;
import cloud.commandframework.exceptions.CommandExecutionException;
import cloud.commandframework.exceptions.InvalidCommandSenderException;
import cloud.commandframework.exceptions.InvalidSyntaxException;
import cloud.commandframework.exceptions.NoPermissionException;
@ -42,6 +43,7 @@ import java.util.concurrent.CompletionException;
public class JavacordCommand<C> implements MessageCreateListener {
private static final String MESSAGE_INTERNAL_ERROR = "An internal error occurred while attempting to perform this command.";
private static final String MESSAGE_NO_PERMS = "I'm sorry, but you do not have the permission to do this :/";
private final JavacordCommandManager<C> manager;
@ -95,6 +97,7 @@ public class JavacordCommand<C> implements MessageCreateListener {
if (throwable == null) {
return;
}
final Throwable finalThrowable = throwable;
if (throwable instanceof CompletionException) {
throwable = throwable.getCause();
@ -153,6 +156,20 @@ public class JavacordCommand<C> implements MessageCreateListener {
return;
}
if (throwable instanceof CommandExecutionException) {
manager.handleException(
sender,
CommandExecutionException.class,
(CommandExecutionException) throwable,
(c, e) -> {
commandSender.sendErrorMessage(MESSAGE_INTERNAL_ERROR);
finalThrowable.getCause().printStackTrace();
}
);
return;
}
commandSender.sendErrorMessage(throwable.getMessage());
throwable.printStackTrace();
});

View file

@ -24,6 +24,7 @@
package cloud.commandframework.jda;
import cloud.commandframework.exceptions.ArgumentParseException;
import cloud.commandframework.exceptions.CommandExecutionException;
import cloud.commandframework.exceptions.InvalidCommandSenderException;
import cloud.commandframework.exceptions.InvalidSyntaxException;
import cloud.commandframework.exceptions.NoPermissionException;
@ -41,6 +42,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
@SuppressWarnings("deprecation")
public class JDACommandListener<C> extends ListenerAdapter {
private static final String MESSAGE_INTERNAL_ERROR = "An internal error occurred while attempting to perform this command.";
private static final String MESSAGE_INVALID_SYNTAX = "Invalid Command Syntax. Correct command syntax is: ";
private static final String MESSAGE_NO_PERMS = "I'm sorry, but you do not have permission to perform this command. "
+ "Please contact the server administrators if you believe that this is in error.";
@ -116,6 +118,16 @@ public class JDACommandListener<C> extends ListenerAdapter {
.getMessage()
)
);
} else if (throwable instanceof CommandExecutionException) {
this.commandManager.handleException(sender, CommandExecutionException.class,
(CommandExecutionException) throwable, (c, e) -> {
this.sendMessage(
event,
MESSAGE_INTERNAL_ERROR
);
throwable.getCause().printStackTrace();
}
);
} else {
this.sendMessage(event, throwable.getMessage());
}