Don't print stack-traces to chat, just inform the user of the error

This commit is contained in:
Brady 2019-09-26 23:18:05 -05:00
parent 1064a79e1d
commit 5ba1e67ea0
No known key found for this signature in database
GPG Key ID: 73A788379A197567
4 changed files with 7 additions and 57 deletions

View File

@ -37,8 +37,11 @@ public interface Helper {
Helper HELPER = new Helper() {};
static ITextComponent getPrefix() {
// Inner text component
ITextComponent baritone = new TextComponentString(BaritoneAPI.getSettings().shortBaritonePrefix.value ? "B" : "Baritone");
baritone.getStyle().setColor(TextFormatting.LIGHT_PURPLE);
// Outer brackets
ITextComponent prefix = new TextComponentString("");
prefix.getStyle().setColor(TextFormatting.DARK_PURPLE);
prefix.appendText("[");

View File

@ -67,7 +67,7 @@ public class ArgParserManager {
* @param type The type to try and parse the argument into.
* @param arg The argument to parse.
* @return An instance of the specified class.
* @throws CommandInvalidTypeException If the parsing failed
* @throws CommandInvalidTypeException If the parsing failed
*/
public static <T> T parseStateless(Class<T> type, CommandArgument arg) throws CommandInvalidTypeException {
IArgParser.Stateless<T> parser = getParserStateless(type);
@ -89,7 +89,7 @@ public class ArgParserManager {
* @param arg The argument to parse.
* @param state The state to pass to the {@link IArgParser.Stated}.
* @return An instance of the specified class.
* @throws CommandInvalidTypeException If the parsing failed
* @throws CommandInvalidTypeException If the parsing failed
* @see IArgParser.Stated
*/
public static <T, S> T parseStated(Class<T> type, Class<S> stateKlass, CommandArgument arg, S state) throws CommandInvalidTypeException {

View File

@ -17,62 +17,10 @@
package baritone.api.utils.command.exception;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CommandUnhandledException extends RuntimeException implements ICommandException {
public CommandUnhandledException(Throwable cause) {
super(String.format(
"An unhandled exception has occurred:\n\n%s",
getFriendlierStackTrace(cause)
));
}
private static String getStackTrace(Throwable throwable) {
StringWriter sw = new StringWriter();
throwable.printStackTrace(new PrintWriter(sw));
return sw.toString();
}
private static String getBaritoneStackTrace(String stackTrace) {
List<String> lines = Stream.of(stackTrace.split("\n"))
.collect(Collectors.toList());
int lastBaritoneLine = 0;
for (int i = 0; i < lines.size(); i++) {
if (lines.get(i).startsWith("\tat baritone.") && lines.get(i).contains("BaritoneChatControl")) {
lastBaritoneLine = i;
}
}
return String.join("\n", lines.subList(0, lastBaritoneLine + 1));
}
private static String getBaritoneStackTrace(Throwable throwable) {
return getBaritoneStackTrace(getStackTrace(throwable));
}
private static String getFriendlierStackTrace(String stackTrace) {
List<String> lines = Arrays.asList(stackTrace.split("\n"));
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
if (line.startsWith("\tat ")) {
if (line.startsWith("\tat baritone.")) {
line = line.replaceFirst("^\tat [a-z.]+?([A-Z])", "\tat $1");
}
// line = line.replaceFirst("\\(([^)]+)\\)$", "\n\t . $1");
line = line.replaceFirst("\\([^:]+:(\\d+)\\)$", ":$1");
line = line.replaceFirst("\\(Unknown Source\\)$", "");
lines.set(i, line);
}
}
return String.join("\n", lines);
}
private static String getFriendlierStackTrace(Throwable throwable) {
return getFriendlierStackTrace(getBaritoneStackTrace(throwable));
super("An unhandled exception occurred. The error is in your game's log, please report this at https://github.com/cabaletta/baritone/issues");
cause.printStackTrace();
}
}

View File

@ -71,7 +71,6 @@ public class CommandExecution {
} catch (CommandException e) {
e.handle(command, args.args);
} catch (Throwable t) {
t.printStackTrace();
new CommandUnhandledException(t).handle(command, args.args);
}
}