SteamWar/SpigotCore
Archiviert
13
0

Improve CommandFramework Exception readability

Dieser Commit ist enthalten in:
yoyosource 2022-01-03 00:28:41 +01:00
Ursprung d9fc0a5a2f
Commit 1d41641b3b
3 geänderte Dateien mit 85 neuen und 4 gelöschten Zeilen

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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());
}
}

Datei anzeigen

@ -96,11 +96,14 @@ public abstract class SWCommand {
void execute(CommandSender sender, String alias, String[] args) { void execute(CommandSender sender, String alias, String[] args) {
try { try {
if (!commandList.stream().anyMatch(s -> s.invoke(sender, args))) { if (!commandList.stream().anyMatch(s -> s.invoke(sender, alias, args))) {
commandHelpList.stream().anyMatch(s -> s.invoke(sender, args)); commandHelpList.stream().anyMatch(s -> s.invoke(sender, alias, args));
} }
} catch (CommandNoHelpException e) { } catch (CommandNoHelpException e) {
// Ignored // Ignored
} catch (CommandFrameworkException e) {
Bukkit.getLogger().log(Level.SEVERE, e.getBuildStackTrace());
throw e;
} }
} }

Datei anzeigen

@ -61,7 +61,7 @@ class SubCommand {
commandSenderFunction = sender -> parameters[0].getType().cast(sender); commandSenderFunction = sender -> parameters[0].getType().cast(sender);
} }
boolean invoke(CommandSender commandSender, String[] args) { boolean invoke(CommandSender commandSender, String alias, String[] args) {
try { try {
if (!commandSenderPredicate.test(commandSender)) { if (!commandSenderPredicate.test(commandSender)) {
return false; return false;
@ -97,7 +97,9 @@ class SubCommand {
throw e; throw e;
} catch (CommandParseException e) { } catch (CommandParseException e) {
return false; 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); throw new SecurityException(e.getMessage(), e);
} }
return true; return true;