Switch to Throwable for stack trace handling

This commit is contained in:
JeremyStar™ 2024-06-11 19:58:29 +02:00
parent c369b164a5
commit 66670b6471
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
5 changed files with 31 additions and 31 deletions

View file

@ -109,10 +109,10 @@ public final class CrashHandler {
* *
* @param logIssuer information about the class that caused the crash * @param logIssuer information about the class that caused the crash
* @param message crash error detail * @param message crash error detail
* @param exception simply to provide stacktrace and further insight into the crash, can be set to {@code null} * @param throwable simply to provide stacktrace and further insight into the crash, can be set to {@code null}
* @since 1-alpha0 * @since 1-alpha0
*/ */
public static void handleCrash(@NotNull LogIssuer logIssuer, @NotNull String message, @Nullable Exception exception) { public static void handleCrash(@NotNull LogIssuer logIssuer, @NotNull String message, @Nullable Throwable throwable) {
crashed = true; crashed = true;
String base = crashTemplate; String base = crashTemplate;
@ -133,7 +133,7 @@ public final class CrashHandler {
temporaryPlaceholders.add(new IssuerPackage(logIssuer)); temporaryPlaceholders.add(new IssuerPackage(logIssuer));
temporaryPlaceholders.add(new IssuerPath(logIssuer)); temporaryPlaceholders.add(new IssuerPath(logIssuer));
// etc // etc
temporaryPlaceholders.add(new Stacktrace(exception)); temporaryPlaceholders.add(new Stacktrace(throwable));
// Replace placeholders // Replace placeholders
base = PlaceholderEngine.getInstance().process(base, temporaryPlaceholders); base = PlaceholderEngine.getInstance().process(base, temporaryPlaceholders);

View file

@ -192,11 +192,11 @@ public final class Logger {
* *
* @param logIssuer issuing class * @param logIssuer issuing class
* @param message diagnostic message * @param message diagnostic message
* @param exception the exception that caused this crash * @param throwable the throwable that caused this crash
* @since 1-alpha0 * @since 1-alpha0
*/ */
public static void crash(@NotNull LogIssuer logIssuer, @NotNull String message, @NotNull Exception exception) { public static void crash(@NotNull LogIssuer logIssuer, @NotNull String message, @NotNull Throwable throwable) {
CrashHandler.handleCrash(logIssuer, message, exception); CrashHandler.handleCrash(logIssuer, message, throwable);
} }
/** /**

View file

@ -119,12 +119,12 @@ public final class LoggerInstance {
* Crashes the entire engine. * Crashes the entire engine.
* *
* @param message the diagnostic message * @param message the diagnostic message
* @param exception the exception that caused this crash * @param throwable the throwable that caused this crash
* @see CrashHandler * @see CrashHandler
* @since 1-alpha0 * @since 1-alpha0
*/ */
public void crash(@NotNull String message, @NotNull Exception exception) { public void crash(@NotNull String message, @NotNull Throwable throwable) {
Logger.crash(logIssuer, message, exception); Logger.crash(logIssuer, message, throwable);
} }
/** /**

View file

@ -33,21 +33,21 @@ import org.jetbrains.annotations.Nullable;
@SuppressWarnings({ "unused" }) @SuppressWarnings({ "unused" })
public final class Stacktrace implements Placeholder { public final class Stacktrace implements Placeholder {
/** /**
* The {@link Exception} to use. * The {@link Throwable} to use.
* *
* @since 1-alpha0 * @since 1-alpha0
*/ */
@Nullable @Nullable
private final Exception exception; private final Throwable throwable;
/** /**
* Constructor. * Constructor.
* *
* @param exception the {@link Exception} to use * @param throwable the {@link Throwable} to use
* @since 1-alpha0 * @since 1-alpha0
*/ */
public Stacktrace(@Nullable Exception exception) { public Stacktrace(@Nullable Throwable throwable) {
this.exception = exception; this.throwable = throwable;
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -59,10 +59,10 @@ public final class Stacktrace implements Placeholder {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public @NotNull String replace(@NotNull String text) { public @NotNull String replace(@NotNull String text) {
if (exception == null) if (throwable == null)
return text.replace("%" + getName() + "%", "No stacktrace is available."); return text.replace("%" + getName() + "%", "No stacktrace is available.");
else { else {
StackTraceParser stackTraceParser = new StackTraceParser(exception); StackTraceParser stackTraceParser = new StackTraceParser(throwable);
return text.replace("%" + getName() + "%", stackTraceParser.getHeader() + "\n" + stackTraceParser.getStackTrace()); return text.replace("%" + getName() + "%", stackTraceParser.getHeader() + "\n" + stackTraceParser.getStackTrace());
} }

View file

@ -24,50 +24,50 @@ import lombok.Getter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Parses the {@link StackTraceElement}s of an {@link Exception} for use in {@link String}. * Parses the {@link StackTraceElement}s of an {@link Throwable} for use in {@link String}.
* *
* @see Exception * @see Throwable
* @since 1-alpha0 * @since 1-alpha0
*/ */
@SuppressWarnings({ "unused", "JavadocBlankLines" }) @SuppressWarnings({ "unused", "JavadocBlankLines" })
public class StackTraceParser { public class StackTraceParser {
/** /**
* The exception to use. * The throwable to use.
* *
* @see Exception * @see Throwable
* @since 1-alpha0 * @since 1-alpha0
* *
* -- GETTER -- * -- GETTER --
* Returns the exception to use. * Returns the throwable to use.
* *
* @see Exception * @see Throwable
* @since 1-alpha0 * @since 1-alpha0
*/ */
@Getter(AccessLevel.PROTECTED) @Getter(AccessLevel.PROTECTED)
private final @NotNull Exception exception; private final @NotNull Throwable throwable;
/** /**
* Constructor. * Constructor.
* *
* @param exception the exception to use * @param throwable the throwable to use
* @since 1-alpha0 * @since 1-alpha0
*/ */
public StackTraceParser(@NotNull Exception exception) { public StackTraceParser(@NotNull Throwable throwable) {
this.exception = exception; this.throwable = throwable;
} }
/** /**
* Returns the stack trace header.<br/> * Returns the stack trace header.<br/>
* It looks like this: {@code Caused by java.lang.NullPointerException: This application can crash!} * It looks like this: {@code Caused by java.lang.NullPointerThrowable: This application can crash!}
* *
* @return the stack trace header * @return the stack trace header
* @since 1-alpha0 * @since 1-alpha0
*/ */
public String getHeader() { public String getHeader() {
if (exception.getMessage() == null) if (throwable.getMessage() == null)
return "Caused by: " + exception.getClass().getName(); return "Caused by: " + throwable.getClass().getName();
else else
return "Caused by: " + exception.getClass().getName() + ": " + exception.getMessage(); return "Caused by: " + throwable.getClass().getName() + ": " + throwable.getMessage();
} }
/** /**
@ -79,7 +79,7 @@ public class StackTraceParser {
public String getStackTrace() { public String getStackTrace() {
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
for (StackTraceElement element : exception.getStackTrace()) { for (StackTraceElement element : throwable.getStackTrace()) {
if (!output.isEmpty()) output.append("\n"); if (!output.isEmpty()) output.append("\n");
output.append("\tat ").append(element.toString()); output.append("\tat ").append(element.toString());
} }