3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 02:05:53 +00:00

Wallet template: some improvements to crash handling.

This commit is contained in:
Mike Hearn 2013-09-22 14:45:05 +02:00
parent 896142504c
commit 6fa719df2a
3 changed files with 28 additions and 8 deletions

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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;