Switch to Throwable for stack trace handling
This commit is contained in:
parent
c369b164a5
commit
66670b6471
5 changed files with 31 additions and 31 deletions
|
@ -109,10 +109,10 @@ public final class CrashHandler {
|
|||
*
|
||||
* @param logIssuer information about the class that caused the crash
|
||||
* @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
|
||||
*/
|
||||
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;
|
||||
String base = crashTemplate;
|
||||
|
||||
|
@ -133,7 +133,7 @@ public final class CrashHandler {
|
|||
temporaryPlaceholders.add(new IssuerPackage(logIssuer));
|
||||
temporaryPlaceholders.add(new IssuerPath(logIssuer));
|
||||
// etc
|
||||
temporaryPlaceholders.add(new Stacktrace(exception));
|
||||
temporaryPlaceholders.add(new Stacktrace(throwable));
|
||||
|
||||
// Replace placeholders
|
||||
base = PlaceholderEngine.getInstance().process(base, temporaryPlaceholders);
|
||||
|
|
|
@ -192,11 +192,11 @@ public final class Logger {
|
|||
*
|
||||
* @param logIssuer issuing class
|
||||
* @param message diagnostic message
|
||||
* @param exception the exception that caused this crash
|
||||
* @param throwable the throwable that caused this crash
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
public static void crash(@NotNull LogIssuer logIssuer, @NotNull String message, @NotNull Exception exception) {
|
||||
CrashHandler.handleCrash(logIssuer, message, exception);
|
||||
public static void crash(@NotNull LogIssuer logIssuer, @NotNull String message, @NotNull Throwable throwable) {
|
||||
CrashHandler.handleCrash(logIssuer, message, throwable);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -119,12 +119,12 @@ public final class LoggerInstance {
|
|||
* Crashes the entire engine.
|
||||
*
|
||||
* @param message the diagnostic message
|
||||
* @param exception the exception that caused this crash
|
||||
* @param throwable the throwable that caused this crash
|
||||
* @see CrashHandler
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
public void crash(@NotNull String message, @NotNull Exception exception) {
|
||||
Logger.crash(logIssuer, message, exception);
|
||||
public void crash(@NotNull String message, @NotNull Throwable throwable) {
|
||||
Logger.crash(logIssuer, message, throwable);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -33,21 +33,21 @@ import org.jetbrains.annotations.Nullable;
|
|||
@SuppressWarnings({ "unused" })
|
||||
public final class Stacktrace implements Placeholder {
|
||||
/**
|
||||
* The {@link Exception} to use.
|
||||
* The {@link Throwable} to use.
|
||||
*
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
@Nullable
|
||||
private final Exception exception;
|
||||
private final Throwable throwable;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param exception the {@link Exception} to use
|
||||
* @param throwable the {@link Throwable} to use
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
public Stacktrace(@Nullable Exception exception) {
|
||||
this.exception = exception;
|
||||
public Stacktrace(@Nullable Throwable throwable) {
|
||||
this.throwable = throwable;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
|
@ -59,10 +59,10 @@ public final class Stacktrace implements Placeholder {
|
|||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public @NotNull String replace(@NotNull String text) {
|
||||
if (exception == null)
|
||||
if (throwable == null)
|
||||
return text.replace("%" + getName() + "%", "No stacktrace is available.");
|
||||
else {
|
||||
StackTraceParser stackTraceParser = new StackTraceParser(exception);
|
||||
StackTraceParser stackTraceParser = new StackTraceParser(throwable);
|
||||
|
||||
return text.replace("%" + getName() + "%", stackTraceParser.getHeader() + "\n" + stackTraceParser.getStackTrace());
|
||||
}
|
||||
|
|
|
@ -24,50 +24,50 @@ import lombok.Getter;
|
|||
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
|
||||
*/
|
||||
@SuppressWarnings({ "unused", "JavadocBlankLines" })
|
||||
public class StackTraceParser {
|
||||
/**
|
||||
* The exception to use.
|
||||
* The throwable to use.
|
||||
*
|
||||
* @see Exception
|
||||
* @see Throwable
|
||||
* @since 1-alpha0
|
||||
*
|
||||
* -- GETTER --
|
||||
* Returns the exception to use.
|
||||
* Returns the throwable to use.
|
||||
*
|
||||
* @see Exception
|
||||
* @see Throwable
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
@Getter(AccessLevel.PROTECTED)
|
||||
private final @NotNull Exception exception;
|
||||
private final @NotNull Throwable throwable;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param exception the exception to use
|
||||
* @param throwable the throwable to use
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
public StackTraceParser(@NotNull Exception exception) {
|
||||
this.exception = exception;
|
||||
public StackTraceParser(@NotNull Throwable throwable) {
|
||||
this.throwable = throwable;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @since 1-alpha0
|
||||
*/
|
||||
public String getHeader() {
|
||||
if (exception.getMessage() == null)
|
||||
return "Caused by: " + exception.getClass().getName();
|
||||
if (throwable.getMessage() == null)
|
||||
return "Caused by: " + throwable.getClass().getName();
|
||||
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() {
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
for (StackTraceElement element : exception.getStackTrace()) {
|
||||
for (StackTraceElement element : throwable.getStackTrace()) {
|
||||
if (!output.isEmpty()) output.append("\n");
|
||||
output.append("\tat ").append(element.toString());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue