Skip to content

Commit

Permalink
map exceptions to ArgumentSyntaxException
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Oct 10, 2024
1 parent 3619d36 commit d0e6e10
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.arguments.ArgumentLiteral;
import net.minestom.server.command.builder.arguments.ArgumentType;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.command.builder.suggestion.SuggestionCallback;
import net.minestom.server.command.builder.suggestion.SuggestionEntry;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import revxrsal.commands.autocomplete.SuggestionProvider;
import revxrsal.commands.command.ExecutableCommand;
import revxrsal.commands.exception.ThrowableFromCommand;
import revxrsal.commands.exception.context.ErrorContext;
import revxrsal.commands.hook.CancelHandle;
import revxrsal.commands.hook.CommandRegisteredHook;
Expand Down Expand Up @@ -280,18 +283,28 @@ private ArgumentColl ofFlag(ParameterNode<A, ?> parameter) {
.map((sender, strings) -> {
A actor = actorFactory.create(sender, command.lamp());
MutableStringStream input = StringStream.createMutable(String.join(" ", strings));
ExecutionContext<A> context = ExecutionContext.create(command, actor, input);
return parameter.parse(input, context);
return parseOrThrow(command, parameter, actor, input);
});
return ArgumentType.String(parameter.name())
.map((sender, s) -> {
A actor = actorFactory.create(sender, command.lamp());
MutableStringStream input = StringStream.createMutable(s);
ExecutionContext<A> context = ExecutionContext.create(command, actor, input);
return parameter.parse(input, context);
return parseOrThrow(command, parameter, actor, input);
});
}

@Nullable
private <T> T parseOrThrow(@NotNull ExecutableCommand<A> command, @NotNull ParameterNode<A, T> parameter, A actor, MutableStringStream input) {
ExecutionContext<A> context = ExecutionContext.create(command, actor, input);
try {
return parameter.parse(input, context);
} catch (Throwable t) {
if (t.getClass().isAnnotationPresent(ThrowableFromCommand.class))
throw new ArgumentSyntaxException(t.getMessage(), context.input().source(), 0);
throw t;
}
}

/**
* Creates a {@link SuggestionCallback} that wraps the parameter's {@link SuggestionProvider}.
*
Expand Down

0 comments on commit d0e6e10

Please sign in to comment.