From 1d41641b3bd4312c28e588811ebe3be07e8badc5 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 3 Jan 2022 00:28:41 +0100 Subject: [PATCH] Improve CommandFramework Exception readability --- .../command/CommandFrameworkException.java | 76 +++++++++++++++++++ .../src/de/steamwar/command/SWCommand.java | 7 +- .../src/de/steamwar/command/SubCommand.java | 6 +- 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/command/CommandFrameworkException.java diff --git a/SpigotCore_Main/src/de/steamwar/command/CommandFrameworkException.java b/SpigotCore_Main/src/de/steamwar/command/CommandFrameworkException.java new file mode 100644 index 0000000..916417d --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/CommandFrameworkException.java @@ -0,0 +1,76 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.command; + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.lang.reflect.InvocationTargetException; + +public class CommandFrameworkException extends RuntimeException { + + private InvocationTargetException invocationTargetException; + private String alias; + private String[] args; + + private String message; + + CommandFrameworkException(InvocationTargetException invocationTargetException, String alias, String[] args) { + this.invocationTargetException = invocationTargetException; + this.alias = alias; + this.args = args; + } + + public synchronized String getBuildStackTrace() { + if (message != null) { + return message; + } + StackTraceElement[] stackTraceElements = invocationTargetException.getCause().getStackTrace(); + StringBuilder st = new StringBuilder(); + st.append(invocationTargetException.getCause().getClass().getTypeName()); + if (invocationTargetException.getCause().getMessage() != null) { + st.append(": ").append(invocationTargetException.getCause().getMessage()); + } + st.append("\n"); + if (alias != null && !alias.isEmpty()) { + st.append("Performed command: ").append(alias).append(" ").append(String.join(" ", args)).append("\n"); + } + for (int i = 0; i < stackTraceElements.length - invocationTargetException.getStackTrace().length; i++) { + st.append("\tat ").append(stackTraceElements[i].toString()).append("\n"); + } + st.append("\n"); + message = st.toString(); + return message; + } + + @Override + public void printStackTrace() { + printStackTrace(System.err); + } + + @Override + public void printStackTrace(PrintStream s) { + s.print(getBuildStackTrace()); + } + + @Override + public void printStackTrace(PrintWriter s) { + s.print(getBuildStackTrace()); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 5715127..6e93262 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -96,11 +96,14 @@ public abstract class SWCommand { void execute(CommandSender sender, String alias, String[] args) { try { - if (!commandList.stream().anyMatch(s -> s.invoke(sender, args))) { - commandHelpList.stream().anyMatch(s -> s.invoke(sender, args)); + if (!commandList.stream().anyMatch(s -> s.invoke(sender, alias, args))) { + commandHelpList.stream().anyMatch(s -> s.invoke(sender, alias, args)); } } catch (CommandNoHelpException e) { // Ignored + } catch (CommandFrameworkException e) { + Bukkit.getLogger().log(Level.SEVERE, e.getBuildStackTrace()); + throw e; } } diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index d95fdf7..3254ade 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -61,7 +61,7 @@ class SubCommand { commandSenderFunction = sender -> parameters[0].getType().cast(sender); } - boolean invoke(CommandSender commandSender, String[] args) { + boolean invoke(CommandSender commandSender, String alias, String[] args) { try { if (!commandSenderPredicate.test(commandSender)) { return false; @@ -97,7 +97,9 @@ class SubCommand { throw e; } catch (CommandParseException e) { return false; - } catch (IllegalAccessException | RuntimeException | InvocationTargetException e) { + } catch (InvocationTargetException e) { + throw new CommandFrameworkException(e, alias, args); + } catch (IllegalAccessException | RuntimeException e) { throw new SecurityException(e.getMessage(), e); } return true;