-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust how throwables and stack trace are preserved
- Loading branch information
1 parent
478c7f7
commit 83a1f72
Showing
3 changed files
with
103 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 43 additions & 2 deletions
45
src/main/java/io/wispforest/owo/util/StackTraceSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,51 @@ | ||
package io.wispforest.owo.util; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public record StackTraceSupplier(StackTraceElement[] stackTrace, Supplier<String> message) implements Supplier<String> { | ||
public final class StackTraceSupplier implements Supplier<String> { | ||
private final Throwable throwable; | ||
private final @Nullable Supplier<String> message; | ||
|
||
private StackTraceSupplier(@Nullable Throwable throwable, @Nullable Supplier<String> message) { | ||
this.throwable = throwable; | ||
this.message = message; | ||
} | ||
|
||
public static StackTraceSupplier of(Throwable throwable) { | ||
return new StackTraceSupplier(throwable, null); | ||
} | ||
|
||
public static StackTraceSupplier of(Throwable throwable, Supplier<String> supplier) { | ||
return new StackTraceSupplier(throwable, supplier); | ||
} | ||
|
||
public static StackTraceSupplier of(String message) { | ||
return new StackTraceSupplier(new IllegalStateException(message), null); | ||
} | ||
|
||
@Override | ||
public String get() { | ||
return message.get(); | ||
return message != null ? message.get() : throwable.getMessage(); | ||
} | ||
|
||
public StackTraceElement[] getFullStackTrace() { | ||
var innerThrowable = throwable(); | ||
while (innerThrowable.getCause() != null) { | ||
innerThrowable = throwable().getCause(); | ||
} | ||
return innerThrowable.getStackTrace(); | ||
} | ||
|
||
public Throwable throwable() { | ||
return throwable; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "StackTraceSupplier[" + | ||
"throwable=" + throwable + ", " + | ||
"message=" + message + ']'; | ||
} | ||
} |