Verified Commit 2868e809 authored by Yannick Schinko's avatar Yannick Schinko
Browse files

Fixes in the Exception base class

And even better exception logging
parent 21a08e6a
Pipeline #393 passed with stages
in 4 minutes and 19 seconds
...@@ -6,10 +6,14 @@ import java.util.Collection; ...@@ -6,10 +6,14 @@ import java.util.Collection;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import lombok.Getter; import lombok.Getter;
import org.slf4j.Logger;
import team.aura_dev.aurasudo.platform.common.AuraSudoBase;
import team.aura_dev.aurasudo.platform.common.permission.Permission; import team.aura_dev.aurasudo.platform.common.permission.Permission;
import team.aura_dev.aurasudo.platform.common.player.PlayerDataCommon; import team.aura_dev.aurasudo.platform.common.player.PlayerDataCommon;
public abstract class BaseCommand { public abstract class BaseCommand {
protected static final Logger logger = AuraSudoBase.logger;
public final Permission BASE; public final Permission BASE;
public final Permission COMMAND; public final Permission COMMAND;
...@@ -60,6 +64,14 @@ public abstract class BaseCommand { ...@@ -60,6 +64,14 @@ public abstract class BaseCommand {
execute(player, alias, argumentList); execute(player, alias, argumentList);
} catch (RuntimeException e) { } catch (RuntimeException e) {
logger.warn(
"Unexpected Exception during command execution of /"
+ alias
+ " (/@id@:"
+ getBaseCommand()
+ "):",
e);
throw new CommandExecutionException(e); throw new CommandExecutionException(e);
} }
} catch (CommandExecutionException e) { } catch (CommandExecutionException e) {
......
...@@ -2,6 +2,9 @@ package team.aura_dev.aurasudo.platform.common.command; ...@@ -2,6 +2,9 @@ package team.aura_dev.aurasudo.platform.common.command;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
...@@ -23,15 +26,16 @@ public class CommandExecutionException extends Exception { ...@@ -23,15 +26,16 @@ public class CommandExecutionException extends Exception {
super(message, cause); super(message, cause);
this.message = this.message =
Component.text(message, NamedTextColor.DARK_RED) Component.empty()
.append(Component.text(message, NamedTextColor.DARK_RED))
.append(Component.newline()) .append(Component.newline())
.append( .append(
Component.text(cause.getMessage()) Component.text(cause.getClass().getName() + ": " + cause.getMessage())
.hoverEvent(Component.text(getStackTrace(cause)).asHoverEvent())); .hoverEvent(Component.text(getStackTrace(cause)).asHoverEvent()));
} }
public CommandExecutionException(Throwable cause) { public CommandExecutionException(Throwable cause) {
this("Unknown error", cause); this("Unexpected Exception:", cause);
} }
public TextComponent getMessageComponent() { public TextComponent getMessageComponent() {
...@@ -43,6 +47,14 @@ public class CommandExecutionException extends Exception { ...@@ -43,6 +47,14 @@ public class CommandExecutionException extends Exception {
PrintWriter pw = new PrintWriter(sw); PrintWriter pw = new PrintWriter(sw);
cause.printStackTrace(pw); cause.printStackTrace(pw);
return pw.toString(); String[] splitStackTrace = sw.toString().replace("\t", " ").trim().split("\r\n|\r|\n", 21);
Stream<String> output = Arrays.stream(splitStackTrace);
if (splitStackTrace.length == 21) {
// Too many lines. Cut them off. Max 20 lines in the final string
output = Stream.concat(output.limit(19), Stream.of("..."));
}
return output.collect(Collectors.joining("\n"));
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment