Improve CommandFramework Exception readability
Dieser Commit ist enthalten in:
Ursprung
d9fc0a5a2f
Commit
1d41641b3b
@ -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());
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren