diff --git a/src/main/java/io/github/stekeblad/videouploader/main/Main.java b/src/main/java/io/github/stekeblad/videouploader/main/Main.java index 0edfc21..5938636 100644 --- a/src/main/java/io/github/stekeblad/videouploader/main/Main.java +++ b/src/main/java/io/github/stekeblad/videouploader/main/Main.java @@ -8,7 +8,6 @@ import io.github.stekeblad.videouploader.utils.translation.Translations; import io.github.stekeblad.videouploader.utils.translation.TranslationsManager; import javafx.application.Application; -import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.stage.Stage; @@ -33,16 +32,7 @@ public void start(Stage primaryStage) { Translations trans = TranslationsManager.getTranslation(TranslationBundles.BASE); // Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught - Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> { - exception.printStackTrace(); - if (Platform.isFxApplicationThread()) { - AlertUtils.exceptionDialog(trans.getString("app_name"), - "Sorry, something went wrong!", exception); - } else { - Platform.runLater(() -> AlertUtils.exceptionDialog(trans.getString("app_name"), - "Sorry, something went wrong!", exception)); - } - }); + Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception)); try { FXMLLoader loader = new FXMLLoader(getClass().getResource("mainWindow.fxml")); diff --git a/src/main/java/io/github/stekeblad/videouploader/main/mainWindowController.java b/src/main/java/io/github/stekeblad/videouploader/main/mainWindowController.java index ddc2307..48c72ca 100644 --- a/src/main/java/io/github/stekeblad/videouploader/main/mainWindowController.java +++ b/src/main/java/io/github/stekeblad/videouploader/main/mainWindowController.java @@ -92,6 +92,10 @@ public class mainWindowController implements IWindowController { * Initialize things when the window is opened, used instead of initialize as that one does not have access to the scene */ public void myInit() { + // Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught + // Is this needed in every window or only in the Main.java file? + Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception)); + // Load Translations transMainWin = TranslationsManager.getTranslation(TranslationBundles.WINDOW_MAIN); transMainWin.autoTranslate(mainWindowPane); diff --git a/src/main/java/io/github/stekeblad/videouploader/utils/AlertUtils.java b/src/main/java/io/github/stekeblad/videouploader/utils/AlertUtils.java index b48bb96..c3df1b7 100644 --- a/src/main/java/io/github/stekeblad/videouploader/utils/AlertUtils.java +++ b/src/main/java/io/github/stekeblad/videouploader/utils/AlertUtils.java @@ -1,5 +1,6 @@ package io.github.stekeblad.videouploader.utils; +import javafx.application.Platform; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.scene.Scene; @@ -171,4 +172,29 @@ public static void exceptionDialog(String header, String content, Throwable exce GridPane pane = makeLongMsgPane(fullContent, false); paneToWindow(pane, header); } + + /** + * Similar to the exceptionDialog method but used when little is known about the error and the context of where it happened. + * This method prints the stacktrace to the console and attempts to show the exception dialog using a generic + * window header and description text. + * + * @param exception the exception to print to the console and attempt to show in the exceptionDialog + */ + public static void unhandledExceptionDialog(Throwable exception) { + if (exception == null) + return; + + exception.printStackTrace(); + try { + if (Platform.isFxApplicationThread()) { + exceptionDialog("Stekeblads Video Uploader", + "Sorry, something went wrong!", exception); + } else { + Platform.runLater(() -> exceptionDialog("Stekeblads Video Uploader", + "Sorry, something went wrong!", exception)); + } + } catch (Exception e) { + // not much to do here, we tried showing the exception dialog but we failed + } + } } diff --git a/src/main/java/io/github/stekeblad/videouploader/windowControllers/LocalizeCategoriesWindowController.java b/src/main/java/io/github/stekeblad/videouploader/windowControllers/LocalizeCategoriesWindowController.java index cf43510..71f3cd3 100644 --- a/src/main/java/io/github/stekeblad/videouploader/windowControllers/LocalizeCategoriesWindowController.java +++ b/src/main/java/io/github/stekeblad/videouploader/windowControllers/LocalizeCategoriesWindowController.java @@ -42,6 +42,9 @@ public class LocalizeCategoriesWindowController implements IWindowController { * Initialize things when the window is opened, used instead of initialize as that one does not have access to the scene */ public void myInit() { + // Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught + Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception)); + // Filter what can be entered into the textFields txt_country.textProperty().addListener((observable, oldValue, newValue) -> { if(! newValue.matches("[A-Za-z]*") || newValue.length() > 2) { diff --git a/src/main/java/io/github/stekeblad/videouploader/windowControllers/ManagePlaylistsWindowController.java b/src/main/java/io/github/stekeblad/videouploader/windowControllers/ManagePlaylistsWindowController.java index f325a57..34add15 100644 --- a/src/main/java/io/github/stekeblad/videouploader/windowControllers/ManagePlaylistsWindowController.java +++ b/src/main/java/io/github/stekeblad/videouploader/windowControllers/ManagePlaylistsWindowController.java @@ -43,6 +43,9 @@ public class ManagePlaylistsWindowController implements IWindowController { * Initialize a few things when the window is opened, used instead of initialize as that one does not have access to the scene */ public void myInit() { + // Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught + Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception)); + // Load Translations transBasic = TranslationsManager.getTranslation(TranslationBundles.BASE); transPlaylistWindow = TranslationsManager.getTranslation(TranslationBundles.WINDOW_PLAYLIST); diff --git a/src/main/java/io/github/stekeblad/videouploader/windowControllers/MetaDataToolWindowController.java b/src/main/java/io/github/stekeblad/videouploader/windowControllers/MetaDataToolWindowController.java index dbef3ce..7973adf 100644 --- a/src/main/java/io/github/stekeblad/videouploader/windowControllers/MetaDataToolWindowController.java +++ b/src/main/java/io/github/stekeblad/videouploader/windowControllers/MetaDataToolWindowController.java @@ -32,6 +32,9 @@ public class MetaDataToolWindowController implements IWindowController { @Override public void myInit() { + // Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught + Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception)); + prog_readingFile.setProgress(ProgressIndicator.INDETERMINATE_PROGRESS); prog_readingFile.setVisible(false); trans_metaDataTool = TranslationsManager.getTranslation(TranslationBundles.WINDOW_TOOL_META); diff --git a/src/main/java/io/github/stekeblad/videouploader/windowControllers/PresetsWindowController.java b/src/main/java/io/github/stekeblad/videouploader/windowControllers/PresetsWindowController.java index 512d722..93def95 100644 --- a/src/main/java/io/github/stekeblad/videouploader/windowControllers/PresetsWindowController.java +++ b/src/main/java/io/github/stekeblad/videouploader/windowControllers/PresetsWindowController.java @@ -67,6 +67,9 @@ public class PresetsWindowController { * Initialize a few things when the window is opened, used instead of initialize as that one does not have access to the scene */ public void myInit(boolean disableLocaleChange) { + // Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught + Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception)); + configManager = ConfigManager.INSTANCE; categoryUtils = CategoryUtils.INSTANCE; diff --git a/src/main/java/io/github/stekeblad/videouploader/windowControllers/SettingsWindowController.java b/src/main/java/io/github/stekeblad/videouploader/windowControllers/SettingsWindowController.java index f9b6e1d..03f6cbe 100644 --- a/src/main/java/io/github/stekeblad/videouploader/windowControllers/SettingsWindowController.java +++ b/src/main/java/io/github/stekeblad/videouploader/windowControllers/SettingsWindowController.java @@ -52,6 +52,9 @@ public class SettingsWindowController implements IWindowController { * Initialize a few things when the window is opened, used instead of initialize as that one does not have access to the scene */ public void myInit() { + // Set the default exception handler, hopefully it can catch some of the exceptions that is not already caught + Thread.setDefaultUncaughtExceptionHandler((thread, exception) -> AlertUtils.unhandledExceptionDialog(exception)); + configManager = ConfigManager.INSTANCE; translationsMeta = new TranslationsMeta(); basicTrans = TranslationsManager.getTranslation(TranslationBundles.BASE);