Add exception messages so invalid input doesn't result in 'Invalid Command Argument: null' (#13)

Co-authored-by: Alexander Söderberg <Sauilitired@users.noreply.github.com>
This commit is contained in:
Jason 2020-09-30 01:44:07 -07:00 committed by GitHub
parent bea9c54841
commit 09e3e7aa13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 10 deletions

View file

@ -23,6 +23,9 @@
//
package cloud.commandframework.arguments.parser;
import cloud.commandframework.arguments.standard.UUIDArgument;
import com.google.common.collect.ImmutableMap;
import com.google.common.reflect.TypeToken;
import cloud.commandframework.annotations.specifier.Completions;
import cloud.commandframework.annotations.specifier.Range;
import cloud.commandframework.arguments.standard.BooleanArgument;
@ -44,6 +47,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Function;
@ -103,7 +107,8 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
StringArgument.StringMode.SINGLE, (context, s) ->
Arrays.asList(options.get(StandardParameters.COMPLETIONS, new String[0]))));
/* Add options to this */
this.registerParserSupplier(TypeToken.get(Boolean.class), options -> new BooleanArgument.BooleanParser<>(false));
this.registerParserSupplier(TypeToken.of(Boolean.class), options -> new BooleanArgument.BooleanParser<>(false));
this.registerParserSupplier(TypeToken.of(UUID.class), options -> new UUIDArgument.UUIDParser<>());
}
@Override

View file

@ -241,5 +241,10 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
public boolean isLiberal() {
return liberal;
}
@Override
public String getMessage() {
return String.format("Could not parse boolean from '%s'.", input);
}
}
}

View file

@ -145,7 +145,7 @@ public final class CharArgument<C> extends CommandArgument<C, Character> {
private final String input;
/**
* Construct a new boolean parse exception
* Construct a new Char parse exception
*
* @param input String input
*/
@ -161,5 +161,10 @@ public final class CharArgument<C> extends CommandArgument<C, Character> {
public @NonNull String getInput() {
return input;
}
@Override
public String getMessage() {
return String.format("'%s' is not a valid character.", input);
}
}
}

View file

@ -112,7 +112,7 @@ public final class UUIDArgument<C> extends CommandArgument<C, UUID> {
}
private static final class UUIDParser<C> implements ArgumentParser<C, UUID> {
public static final class UUIDParser<C> implements ArgumentParser<C, UUID> {
@Override
public @NonNull ArgumentParseResult<UUID> parse(
@ -141,14 +141,20 @@ public final class UUIDArgument<C> extends CommandArgument<C, UUID> {
public static final class UUIDParseException extends IllegalArgumentException {
private final String input;
/**
* Construct a new example parse exception
* Construct a new UUID parse exception
*
* @param input String input
*/
public UUIDParseException(@NonNull final String input) {
super(input);
this.input = input;
}
@Override
public String getMessage() {
return String.format("Could not parse UUID from '%s'.", input);
}
}
}