From 4b64169b593b9f7ec8900642471773a26a4c5b2d Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Mon, 11 Dec 2017 16:04:22 +0100 Subject: [PATCH] Catch any runtime exceptions from parsing --- build.gradle | 2 +- .../java/com/mojang/brigadier/CommandDispatcher.java | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 2a9aa2a2..cb45c81c 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ import groovy.io.FileType apply plugin: 'java-library' apply plugin: 'maven' -version = '0.1.13' +version = '0.1.14' group = 'com.mojang' task wrapper(type: Wrapper) { diff --git a/src/main/java/com/mojang/brigadier/CommandDispatcher.java b/src/main/java/com/mojang/brigadier/CommandDispatcher.java index 9942a210..392e55d4 100644 --- a/src/main/java/com/mojang/brigadier/CommandDispatcher.java +++ b/src/main/java/com/mojang/brigadier/CommandDispatcher.java @@ -10,6 +10,7 @@ import com.mojang.brigadier.context.CommandContextBuilder; import com.mojang.brigadier.context.StringRange; import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType; import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; @@ -33,7 +34,7 @@ public class CommandDispatcher { public static final SimpleCommandExceptionType ERROR_UNKNOWN_COMMAND = new SimpleCommandExceptionType("command.unknown.command", "Unknown command"); public static final SimpleCommandExceptionType ERROR_UNKNOWN_ARGUMENT = new SimpleCommandExceptionType("command.unknown.argument", "Incorrect argument for command"); public static final SimpleCommandExceptionType ERROR_EXPECTED_ARGUMENT_SEPARATOR = new SimpleCommandExceptionType("command.expected.separator", "Expected whitespace to end one argument, but found trailing data"); - public static final SimpleCommandExceptionType ERROR_COMMAND_FAILED = new SimpleCommandExceptionType("command.failed", "Expected whitespace to end one argument, but found trailing data"); + public static final ParameterizedCommandExceptionType ERROR_PARSE_EXCEPTION = new ParameterizedCommandExceptionType("command.exception", "Could not parse command: ${message}", "message"); public static final String ARGUMENT_SEPARATOR = " "; public static final char ARGUMENT_SEPARATOR_CHAR = ' '; @@ -166,7 +167,11 @@ private ParseResults parseNodes(final CommandNode node, final StringReader final CommandContextBuilder context = contextSoFar.copy(); final StringReader reader = new StringReader(originalReader); try { - child.parse(reader, context); + try { + child.parse(reader, context); + } catch (final RuntimeException ex) { + throw ERROR_PARSE_EXCEPTION.createWithContext(reader, ex.getMessage()); + } if (reader.canRead()) { if (reader.peek() != ARGUMENT_SEPARATOR_CHAR) { throw ERROR_EXPECTED_ARGUMENT_SEPARATOR.createWithContext(reader);