Skip to content

Commit

Permalink
Adjust exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Feb 21, 2024
1 parent 480f5fa commit 15d0114
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.google.inject.Singleton;
import io.leangen.geantyref.TypeToken;
import java.util.List;
import java.util.function.UnaryOperator;
import java.util.function.Function;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.incendo.cloud.Command;
Expand Down Expand Up @@ -87,12 +87,12 @@ public void registerCommands() {
}
}

public void register(final Command.Builder<Commander> builder) {
public void register(final Command.Builder<? extends Commander> builder) {
this.commandManager.command(builder);
}

public void registerSubcommand(final UnaryOperator<Command.Builder<Commander>> builderModifier) {
this.commandManager.command(builderModifier.apply(this.rootBuilder()));
public void registerSubcommand(final Function<Command.Builder<Commander>, Command.Builder<? extends Commander>> builderModifier) {
this.register(builderModifier.apply(this.rootBuilder()));
}

public Command.Builder<Commander> rootBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static net.kyori.adventure.text.format.NamedTextColor.GRAY;
import static net.kyori.adventure.text.format.NamedTextColor.WHITE;
import static net.kyori.adventure.text.format.TextDecoration.ITALIC;
import static org.incendo.cloud.exception.handling.ExceptionHandler.unwrappingHandler;
import static xyz.jpenilla.squaremap.common.util.Components.highlightSpecialCharacters;
import static xyz.jpenilla.squaremap.common.util.Components.placeholder;

Expand All @@ -51,24 +52,26 @@ private ExceptionHandler() {
}

public void registerExceptionHandlers(final CommandManager<Commander> manager) {
manager.exceptionController().registerHandler(CommandExecutionException.class, this::commandExecution);
manager.exceptionController().registerHandler(NoPermissionException.class, this::noPermission);
manager.exceptionController().registerHandler(ArgumentParseException.class, this::argumentParsing);
manager.exceptionController().registerHandler(InvalidCommandSenderException.class, this::invalidSender);
manager.exceptionController().registerHandler(InvalidSyntaxException.class, this::invalidSyntax);
manager.exceptionController()
.registerHandler(CommandExecutionException.class, unwrappingHandler(CommandCompleted.class))
.registerHandler(CommandCompleted.class, this::commandCompleted)
.registerHandler(CommandExecutionException.class, this::commandExecution)
.registerHandler(NoPermissionException.class, this::noPermission)
.registerHandler(ArgumentParseException.class, this::argumentParsing)
.registerHandler(InvalidCommandSenderException.class, this::invalidSender)
.registerHandler(InvalidSyntaxException.class, this::invalidSyntax);
}

private void commandCompleted(final ExceptionContext<Commander, CommandCompleted> ctx) {
final @Nullable Component message = ctx.exception().componentMessage();
if (message != null) {
decorateAndSend(ctx.context().sender(), message);
}
}

private void commandExecution(final ExceptionContext<Commander, CommandExecutionException> ctx) {
final Throwable cause = ctx.exception().getCause();

if (cause instanceof CommandCompleted completed) {
final @Nullable Component message = completed.componentMessage();
if (message != null) {
ctx.context().sender().sendMessage(message);
}
return;
}

Logging.logger().warn("An unexpected error occurred during command execution", cause);

final TextComponent.Builder message = text();
Expand Down

0 comments on commit 15d0114

Please sign in to comment.