From 6fa719df2a1e30383ec4ddb529c73c7a835a6aa4 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Sun, 22 Sep 2013 14:45:05 +0200 Subject: [PATCH] Wallet template: some improvements to crash handling. --- .../src/main/java/wallettemplate/Main.java | 9 ++++--- .../wallettemplate/SendMoneyController.java | 2 +- .../java/wallettemplate/utils/GuiUtils.java | 25 ++++++++++++++++--- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/wallettemplate/src/main/java/wallettemplate/Main.java b/wallettemplate/src/main/java/wallettemplate/Main.java index 25fefa1e..29fd7ceb 100644 --- a/wallettemplate/src/main/java/wallettemplate/Main.java +++ b/wallettemplate/src/main/java/wallettemplate/Main.java @@ -7,6 +7,7 @@ import com.google.bitcoin.params.RegTestParams; import com.google.bitcoin.store.BlockStoreException; import com.google.bitcoin.utils.BriefLogFormatter; import com.google.bitcoin.utils.Threading; +import com.google.common.base.Throwables; import javafx.application.Application; import javafx.application.Platform; import javafx.fxml.FXMLLoader; @@ -35,16 +36,16 @@ public class Main extends Application { @Override public void start(Stage mainWindow) throws Exception { instance = this; + GuiUtils.handleCrashesOnThisThread(); try { init(mainWindow); } catch (Throwable t) { - final Throwable cause = t.getCause(); - if (cause != null && cause.getCause() instanceof BlockStoreException) { + // Nicer message for the case where the block store file is locked. + if (Throwables.getRootCause(t) instanceof BlockStoreException) { GuiUtils.informationalAlert("Already running", "This application is already running and cannot be started twice."); } else { - GuiUtils.crashAlert(t.getLocalizedMessage()); + throw t; } - Platform.exit(); } } diff --git a/wallettemplate/src/main/java/wallettemplate/SendMoneyController.java b/wallettemplate/src/main/java/wallettemplate/SendMoneyController.java index eaa69367..4b354f30 100644 --- a/wallettemplate/src/main/java/wallettemplate/SendMoneyController.java +++ b/wallettemplate/src/main/java/wallettemplate/SendMoneyController.java @@ -52,7 +52,7 @@ public class SendMoneyController { @Override public void onFailure(Throwable t) { // We died trying to empty the wallet. - Platform.runLater(() -> GuiUtils.crashAlert(t.getMessage())); + GuiUtils.crashAlert(t); } }); sendBtn.setDisable(true); diff --git a/wallettemplate/src/main/java/wallettemplate/utils/GuiUtils.java b/wallettemplate/src/main/java/wallettemplate/utils/GuiUtils.java index c686eb28..d1c5d371 100644 --- a/wallettemplate/src/main/java/wallettemplate/utils/GuiUtils.java +++ b/wallettemplate/src/main/java/wallettemplate/utils/GuiUtils.java @@ -1,6 +1,8 @@ package wallettemplate.utils; +import com.google.common.base.Throwables; import javafx.animation.*; +import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.Node; import javafx.scene.Scene; @@ -33,12 +35,29 @@ public class GuiUtils { } } - public static void crashAlert(String alert) { - runAlert((stage, controller) -> controller.crashAlert(stage, alert)); + public static void crashAlert(Throwable t) { + t.printStackTrace(); + Throwable rootCause = Throwables.getRootCause(t); + Runnable r = () -> runAlert((stage, controller) -> controller.crashAlert(stage, rootCause.toString())); + if (Platform.isFxApplicationThread()) + r.run(); + else + Platform.runLater(r); + } + + /** Show a GUI alert box for any unhandled exceptions that propagate out of this thread. */ + public static void handleCrashesOnThisThread() { + Thread.currentThread().setUncaughtExceptionHandler((thread, exception) -> { + GuiUtils.crashAlert(Throwables.getRootCause(exception)); + }); } public static void informationalAlert(String message, String details) { - runAlert((stage, controller) -> controller.informational(stage, message, details)); + Runnable r = () -> runAlert((stage, controller) -> controller.informational(stage, message, details)); + if (Platform.isFxApplicationThread()) + r.run(); + else + Platform.runLater(r); } private static final int UI_ANIMATION_TIME_MSEC = 350;