GuiUtils: replace Guava Throwables.getRootCause() with private helper method

Private helper walks up the chain and avoids loops.
This commit is contained in:
Sean Gilligan 2023-03-25 17:21:37 -07:00 committed by Andreas Schildbach
parent 66d96ab29d
commit b7cf58ba6d

View file

@ -16,7 +16,6 @@
package org.bitcoinj.walletfx.utils; package org.bitcoinj.walletfx.utils;
import com.google.common.base.Throwables;
import javafx.animation.*; import javafx.animation.*;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
@ -30,6 +29,7 @@ import javafx.util.Duration;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.Objects;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import static org.bitcoinj.base.internal.Preconditions.checkState; import static org.bitcoinj.base.internal.Preconditions.checkState;
@ -57,7 +57,7 @@ public class GuiUtils {
public static void crashAlert(Throwable t) { public static void crashAlert(Throwable t) {
t.printStackTrace(); t.printStackTrace();
Throwable rootCause = Throwables.getRootCause(t); Throwable rootCause = findRootCause(t);
Runnable r = () -> { Runnable r = () -> {
runAlert((stage, controller) -> controller.crashAlert(stage, rootCause.toString())); runAlert((stage, controller) -> controller.crashAlert(stage, rootCause.toString()));
Platform.exit(); Platform.exit();
@ -70,7 +70,7 @@ public class GuiUtils {
/** Show a GUI alert box for any unhandled exceptions that propagate out of this thread. */ /** Show a GUI alert box for any unhandled exceptions that propagate out of this thread. */
public static void handleCrashesOnThisThread() { public static void handleCrashesOnThisThread() {
Thread.currentThread().setUncaughtExceptionHandler((thread, exception) -> GuiUtils.crashAlert(Throwables.getRootCause(exception))); Thread.currentThread().setUncaughtExceptionHandler((thread, exception) -> GuiUtils.crashAlert(findRootCause(exception)));
} }
public static void informationalAlert(String message, String details, Object... args) { public static void informationalAlert(String message, String details, Object... args) {
@ -181,4 +181,12 @@ public class GuiUtils {
public static void checkGuiThread() { public static void checkGuiThread() {
checkState(Platform.isFxApplicationThread()); checkState(Platform.isFxApplicationThread());
} }
private static Throwable findRootCause(Throwable throwable) {
Throwable rootCause = Objects.requireNonNull(throwable);
while (rootCause.getCause() != null && rootCause.getCause() != rootCause) {
rootCause = rootCause.getCause();
}
return rootCause;
}
} }