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

@ -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) {
command.getCommandExecutionHandler().execute(commandContext);
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) {
command.getCommandExecutionHandler().execute(commandContext);
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));
}