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

@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added a method to get the failure reason of SelectorParseExceptions
- Added some methods to FlagContext to work with flag values as optionals
- Allow for use of named suggestion providers with `@Flag`s (cloud-annotations)
- Added `CommandExecutionException` which wraps any exception thrown during the execution of command handlers. Should be
handled using `CommandManager#registerExceptionHandler`, similar to `NoSuchCommandException`, `ArgumentParseException`, etc.
### Changed
- Allow for use of `@Completions` annotation with argument types other than String

View file

@ -28,6 +28,7 @@ import cloud.commandframework.annotations.injection.ParameterInjectorRegistry;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.flags.FlagContext;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.CommandExecutionException;
import cloud.commandframework.execution.CommandExecutionHandler;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -115,8 +116,10 @@ class MethodCommandExecutionHandler<C> implements CommandExecutionHandler<C> {
/* Invoke the command method */
try {
this.methodHandle.invokeWithArguments(arguments);
} catch (final Throwable e) {
e.printStackTrace();
} catch (final Error e) {
throw e;
} catch (final Throwable throwable) {
throw new CommandExecutionException(throwable);
}
}

View file

@ -0,0 +1,46 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.exceptions;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* Exception thrown when there is an exception during execution of a command handler
*
* @since 1.2.0
*/
public class CommandExecutionException extends IllegalArgumentException {
private static final long serialVersionUID = -4785446899438294661L;
/**
* Exception thrown when there is an exception during execution of a command handler
*
* @param cause Exception thrown during the execution of a command handler
*/
public CommandExecutionException(final @NonNull Throwable cause) {
super(cause);
}
}

View file

@ -27,6 +27,7 @@ import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.CommandTree;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.CommandExecutionException;
import cloud.commandframework.services.State;
import cloud.commandframework.types.tuples.Pair;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -76,10 +77,17 @@ public final class AsynchronousCommandExecutionCoordinator<C> extends CommandExe
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> input
) {
final CompletableFuture<CommandResult<C>> resultFuture = new CompletableFuture<>();
final Consumer<Command<C>> commandConsumer = command -> {
if (this.commandManager.postprocessContext(commandContext, command) == State.ACCEPTED) {
try {
command.getCommandExecutionHandler().execute(commandContext);
} catch (final CommandExecutionException exception) {
resultFuture.completeExceptionally(exception);
} catch (final Exception exception) {
resultFuture.completeExceptionally(new CommandExecutionException(exception));
}
}
};
@ -97,8 +105,6 @@ public final class AsynchronousCommandExecutionCoordinator<C> extends CommandExe
}, this.executor);
}
final CompletableFuture<CommandResult<C>> resultFuture = new CompletableFuture<>();
this.executor.execute(() -> {
try {
final @NonNull Pair<@Nullable Command<C>, @Nullable Exception> pair =

View file

@ -26,6 +26,7 @@ package cloud.commandframework.execution;
import cloud.commandframework.Command;
import cloud.commandframework.CommandTree;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.CommandExecutionException;
import cloud.commandframework.services.State;
import cloud.commandframework.types.tuples.Pair;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -103,7 +104,7 @@ public abstract class CommandExecutionCoordinator<C> {
}
@Override
public CompletableFuture<CommandResult<C>> coordinateExecution(
public @NonNull CompletableFuture<CommandResult<C>> coordinateExecution(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> input
) {
@ -116,7 +117,13 @@ public abstract class CommandExecutionCoordinator<C> {
} else {
final Command<C> command = Objects.requireNonNull(pair.getFirst());
if (this.getCommandTree().getCommandManager().postprocessContext(commandContext, command) == State.ACCEPTED) {
try {
command.getCommandExecutionHandler().execute(commandContext);
} catch (final CommandExecutionException exception) {
completableFuture.completeExceptionally(exception);
} catch (final Exception exception) {
completableFuture.completeExceptionally(new CommandExecutionException(exception));
}
}
completableFuture.complete(new CommandResult<>(commandContext));
}

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());
}

View file

@ -24,6 +24,7 @@
package cloud.commandframework.pircbotx;
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;
@ -34,6 +35,7 @@ import org.pircbotx.hooks.types.GenericMessageEvent;
final class CloudListenerAdapter<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.";
@ -57,6 +59,7 @@ final class CloudListenerAdapter<C> extends ListenerAdapter {
if (throwable == null) {
return;
}
final Throwable finalThrowable = throwable;
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
@ -90,6 +93,13 @@ final class CloudListenerAdapter<C> extends ListenerAdapter {
"Invalid Command Argument: " + throwable.getCause().getMessage()
)
);
} else if (throwable instanceof CommandExecutionException) {
this.manager.handleException(sender, CommandExecutionException.class,
(CommandExecutionException) throwable, (c, e) -> {
event.respondWith(MESSAGE_INTERNAL_ERROR);
finalThrowable.getCause().printStackTrace();
}
);
} else {
event.respondWith(throwable.getMessage());
}

View file

@ -26,6 +26,7 @@ package cloud.commandframework.bukkit;
import cloud.commandframework.Command;
import cloud.commandframework.arguments.CommandArgument;
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;
@ -38,9 +39,12 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.List;
import java.util.concurrent.CompletionException;
import java.util.logging.Level;
final class BukkitCommand<C> extends org.bukkit.command.Command implements PluginIdentifiableCommand {
private static final String MESSAGE_INTERNAL_ERROR = ChatColor.RED
+ "An internal error occurred while attempting to perform this command.";
private static final String MESSAGE_NO_PERMS = ChatColor.RED
+ "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.";
@ -50,7 +54,6 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
private final BukkitCommandManager<C> manager;
private final Command<C> cloudCommand;
@SuppressWarnings("unchecked")
BukkitCommand(
final @NonNull String label,
final @NonNull List<@NonNull String> aliases,
@ -133,9 +136,25 @@ final class BukkitCommand<C> extends org.bukkit.command.Command implements Plugi
+ ChatColor.GRAY + finalThrowable.getCause()
.getMessage())
);
} else if (throwable instanceof CommandExecutionException) {
this.manager.handleException(sender,
CommandExecutionException.class,
(CommandExecutionException) throwable, (c, e) -> {
commandSender.sendMessage(MESSAGE_INTERNAL_ERROR);
this.manager.getOwningPlugin().getLogger().log(
Level.SEVERE,
"Exception executing command handler",
finalThrowable.getCause()
);
}
);
} else {
commandSender.sendMessage(throwable.getMessage());
throwable.printStackTrace();
commandSender.sendMessage(MESSAGE_INTERNAL_ERROR);
this.manager.getOwningPlugin().getLogger().log(
Level.SEVERE,
"An unhandled exception was thrown during command execution",
throwable
);
}
}
});

View file

@ -26,6 +26,7 @@ package cloud.commandframework.bungee;
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;
@ -38,9 +39,11 @@ import net.md_5.bungee.api.plugin.TabExecutor;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.concurrent.CompletionException;
import java.util.logging.Level;
public final class BungeeCommand<C> extends Command implements TabExecutor {
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 permission to perform this command. "
+ "Please contact the server administrators if you believe that this is in error.";
@ -138,18 +141,27 @@ public final class BungeeCommand<C> extends Command implements TabExecutor {
.getMessage())
.create())
);
} else {
commandSender.sendMessage(new ComponentBuilder(throwable.getMessage()).create());
this.manager.getOwningPlugin().getLogger().warning(
String.format(
"(Cloud) Unknown exception type '%s' with cause '%s'",
throwable.getClass().getCanonicalName(),
throwable.getCause() == null ? "none"
: throwable.getCause()
.getClass().getCanonicalName()
)
} else if (throwable instanceof CommandExecutionException) {
this.manager.handleException(sender,
CommandExecutionException.class,
(CommandExecutionException) throwable, (c, e) -> {
commandSender.sendMessage(new ComponentBuilder(MESSAGE_INTERNAL_ERROR)
.color(ChatColor.RED)
.create());
this.manager.getOwningPlugin().getLogger().log(
Level.SEVERE,
"Exception executing command handler",
finalThrowable.getCause()
);
}
);
} else {
commandSender.sendMessage(new ComponentBuilder(MESSAGE_INTERNAL_ERROR).color(ChatColor.RED).create());
this.manager.getOwningPlugin().getLogger().log(
Level.SEVERE,
"An unhandled exception was thrown during command execution",
throwable
);
throwable.printStackTrace();
}
}
});

View file

@ -26,6 +26,7 @@ package cloud.commandframework.cloudburst;
import cloud.commandframework.Command;
import cloud.commandframework.arguments.CommandArgument;
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 java.util.concurrent.CompletionException;
final class CloudburstCommand<C> extends PluginCommand<Plugin> {
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 permission to perform this command. "
+ "Please contact the server administrators if you believe that this is in error.";
@ -124,9 +126,23 @@ final class CloudburstCommand<C> extends PluginCommand<Plugin> {
"Invalid Command Argument: "
+ finalThrowable.getCause().getMessage())
);
} else if (throwable instanceof CommandExecutionException) {
this.manager.handleException(sender,
CommandExecutionException.class,
(CommandExecutionException) throwable, (c, e) -> {
commandSender.sendMessage(MESSAGE_INTERNAL_ERROR);
manager.getOwningPlugin().getLogger().error(
"Exception executing command handler",
finalThrowable.getCause()
);
}
);
} else {
commandSender.sendMessage(throwable.getMessage());
throwable.printStackTrace();
commandSender.sendMessage(MESSAGE_INTERNAL_ERROR);
manager.getOwningPlugin().getLogger().error(
"An unhandled exception was thrown during command execution",
throwable
);
}
}
});

View file

@ -25,6 +25,7 @@ package cloud.commandframework.minecraft.extras;
import cloud.commandframework.CommandManager;
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;
@ -89,6 +90,19 @@ public final class MinecraftExceptionHandler<C> {
.append(Component.text("Invalid command argument: ", NamedTextColor.RED))
.append(Component.text(e.getCause().getMessage(), NamedTextColor.GRAY))
.build();
/**
* Default component builder for {@link CommandExecutionException}
*
* @since 1.2.0
*/
public static final Function<Exception, Component> DEFAULT_COMMAND_EXECUTION_FUNCTION =
e -> {
e.getCause().printStackTrace();
return Component.text()
.append(Component.text("An internal error occurred while attempting to perform this command.",
NamedTextColor.RED))
.build();
};
private final Map<ExceptionType, BiFunction<C, Exception, Component>> componentBuilders = new HashMap<>();
private Function<Component, Component> decorator = Function.identity();
@ -130,7 +144,17 @@ public final class MinecraftExceptionHandler<C> {
}
/**
* Use all four of the default exception handlers
* Use the default {@link CommandExecutionException} handler
*
* @return {@code this}
* @since 1.2.0
*/
public @NonNull MinecraftExceptionHandler<C> withCommandExecutionHandler() {
return this.withHandler(ExceptionType.COMMAND_EXECUTION, DEFAULT_COMMAND_EXECUTION_FUNCTION);
}
/**
* Use all of the default exception handlers
*
* @return {@code this}
*/
@ -139,7 +163,8 @@ public final class MinecraftExceptionHandler<C> {
.withArgumentParsingHandler()
.withInvalidSenderHandler()
.withInvalidSyntaxHandler()
.withNoPermissionHandler();
.withNoPermissionHandler()
.withCommandExecutionHandler();
}
/**
@ -237,6 +262,15 @@ public final class MinecraftExceptionHandler<C> {
)
);
}
if (componentBuilders.containsKey(ExceptionType.COMMAND_EXECUTION)) {
manager.registerExceptionHandler(
CommandExecutionException.class,
(c, e) -> audienceMapper.apply(c).sendMessage(
Identity.nil(),
this.decorator.apply(this.componentBuilders.get(ExceptionType.COMMAND_EXECUTION).apply(c, e))
)
);
}
}
@ -259,7 +293,13 @@ public final class MinecraftExceptionHandler<C> {
/**
* An argument failed to parse ({@link ArgumentParseException})
*/
ARGUMENT_PARSING
ARGUMENT_PARSING,
/**
* A command handler had an exception ({@link CommandExecutionException})
*
* @since 1.2.0
*/
COMMAND_EXECUTION
}
}

View file

@ -24,6 +24,7 @@
package cloud.commandframework.velocity;
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.function.BiConsumer;
final class VelocityExecutor<C> implements Command<CommandSource> {
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 permission to perform this command. "
+ "Please contact the server administrators if you believe that this is in error.";
@ -128,8 +130,7 @@ final class VelocityExecutor<C> implements Command<CommandSource> {
sender,
ArgumentParseException.class,
(ArgumentParseException) throwable,
(c, e) ->
source.sendMessage(
(c, e) -> source.sendMessage(
Identity.nil(),
Component.text()
.append(Component.text(
@ -142,10 +143,26 @@ final class VelocityExecutor<C> implements Command<CommandSource> {
))
)
);
} else if (throwable instanceof CommandExecutionException) {
this.manager.handleException(
sender,
CommandExecutionException.class,
(CommandExecutionException) throwable,
(c, e) -> {
source.sendMessage(
Identity.nil(),
Component.text(
MESSAGE_INTERNAL_ERROR,
NamedTextColor.RED
)
);
finalThrowable.getCause().printStackTrace();
}
);
} else {
source.sendMessage(
Identity.nil(),
Component.text(throwable.getMessage(), NamedTextColor.RED)
Component.text(MESSAGE_INTERNAL_ERROR, NamedTextColor.RED)
);
throwable.printStackTrace();
}

View file

@ -195,6 +195,7 @@ public final class ExamplePlugin extends JavaPlugin {
.withInvalidSenderHandler()
.withNoPermissionHandler()
.withArgumentParsingHandler()
.withCommandExecutionHandler()
.withDecorator(
component -> Component.text()
.append(Component.text("[", NamedTextColor.DARK_GRAY))