From 4ddacd10254eb04cffd0aaeefb6edb0800b3ecbd Mon Sep 17 00:00:00 2001 From: Christian Pesch Date: Tue, 10 Sep 2024 08:01:52 +0200 Subject: [PATCH] avoiding to display an error dialog when computer is offline and finding places fails https://forum.routeconverter.com/thread-3929.html --- .../java/slash/navigation/gui/actions/DialogAction.java | 2 ++ .../java/slash/navigation/gui/helpers/WindowHelper.java | 8 ++++---- .../main/java/slash/common/helpers/ExceptionHelper.java | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/common-gui/src/main/java/slash/navigation/gui/actions/DialogAction.java b/common-gui/src/main/java/slash/navigation/gui/actions/DialogAction.java index 1d06b272a..d1f36ef50 100644 --- a/common-gui/src/main/java/slash/navigation/gui/actions/DialogAction.java +++ b/common-gui/src/main/java/slash/navigation/gui/actions/DialogAction.java @@ -20,6 +20,8 @@ package slash.navigation.gui.actions; +import slash.common.helpers.ExceptionHelper; + import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; diff --git a/common-gui/src/main/java/slash/navigation/gui/helpers/WindowHelper.java b/common-gui/src/main/java/slash/navigation/gui/helpers/WindowHelper.java index 9479a13ac..11ba62772 100644 --- a/common-gui/src/main/java/slash/navigation/gui/helpers/WindowHelper.java +++ b/common-gui/src/main/java/slash/navigation/gui/helpers/WindowHelper.java @@ -33,8 +33,7 @@ import static javax.swing.JOptionPane.ERROR_MESSAGE; import static javax.swing.JOptionPane.showMessageDialog; import static javax.swing.SwingUtilities.invokeLater; -import static slash.common.helpers.ExceptionHelper.getLocalizedMessage; -import static slash.common.helpers.ExceptionHelper.printStackTrace; +import static slash.common.helpers.ExceptionHelper.*; import static slash.common.system.Platform.getMaximumMemory; /** @@ -68,10 +67,11 @@ public static void handleOutOfMemoryError(OutOfMemoryError e) { } public static void handleThrowable(Class clazz, ActionEvent e, Throwable throwable) { - log.severe(format("Unhandled throwable in action %s from event %s: %s, %s", clazz.getName(), e, throwable, printStackTrace(throwable))); + String stacktrace = isComputerOffline(throwable) ? "" : printStackTrace(throwable); + log.severe(format("Unhandled throwable in action %s from event %s: %s, %s", clazz.getName(), e, getLocalizedMessage(throwable), stacktrace)); showMessageDialog(getFrame(), MessageFormat.format(Application.getInstance().getContext().getBundle(). - getString("unhandled-throwable-error"), clazz.getSimpleName(), getLocalizedMessage(throwable), printStackTrace(throwable)), + getString("unhandled-throwable-error"), clazz.getName(), getLocalizedMessage(throwable), stacktrace), getFrame().getTitle(), ERROR_MESSAGE); } } diff --git a/common/src/main/java/slash/common/helpers/ExceptionHelper.java b/common/src/main/java/slash/common/helpers/ExceptionHelper.java index 805d41a16..5cae5efb0 100644 --- a/common/src/main/java/slash/common/helpers/ExceptionHelper.java +++ b/common/src/main/java/slash/common/helpers/ExceptionHelper.java @@ -40,7 +40,7 @@ public static boolean isComputerOffline(Throwable throwable) { } public static String getLocalizedMessage(Throwable throwable) { - if (throwable instanceof UnknownHostException) + if (isComputerOffline(throwable)) return "Your computer is not connected to the Internet and\n" + "cannot access " + throwable.getMessage() + "."; return throwable.getLocalizedMessage() != null ? throwable.getLocalizedMessage() : throwable.toString();