GUI: added new error dialog for more use cases

This commit is contained in:
Oleg Agafonov 2024-10-11 16:38:46 +04:00
parent 4f9c158c40
commit bad782244f
2 changed files with 26 additions and 5 deletions

View file

@ -1578,6 +1578,7 @@ public class MageFrame extends javax.swing.JFrame implements MageClient {
// FIRST GUI CALL (create main window with all prepared frames, dialogs, etc)
try {
instance = new MageFrame();
EDTExceptionHandler.registerMainApp(instance);
} catch (Throwable e) {
LOGGER.fatal("Critical error on start up, app will be closed: " + e.getMessage(), e);
System.exit(1);

View file

@ -1,17 +1,23 @@
package mage.client.util;
import mage.client.MageFrame;
import org.apache.log4j.Logger;
import java.util.HashMap;
import java.util.Map;
/**
* GUI helper class - catch all app's unhandled errors
*
* @author BetaSteward_at_googlemail.com
* @author BetaSteward_at_googlemail.com, JayDi85
*/
public class EDTExceptionHandler implements Thread.UncaughtExceptionHandler {
private static final Logger logger = Logger.getLogger(EDTExceptionHandler.class);
private static final Map<String, Integer> foundErrors = new HashMap<>();
private static MageFrame mainApp = null; // app to show error dialogs
@Override
public void uncaughtException(Thread t, Throwable e) {
handle(e);
@ -20,8 +26,18 @@ public class EDTExceptionHandler implements Thread.UncaughtExceptionHandler {
public void handle(Throwable throwable) {
try {
logger.fatal("MAGE Client UI error", throwable);
// JOptionPane.showMessageDialog(MageFrame.getDesktop(), throwable, "MAGE Client UI error", JOptionPane.ERROR_MESSAGE);
} catch (Throwable t) {}
// show error dialog for better users feedback about client side errors
// with some protection from dialogs spam on screen (e.g. on render/graphic errors)
String errorKey = throwable.toString();
int foundCount = foundErrors.getOrDefault(errorKey, 0);
if (foundCount < 5 && mainApp != null) {
mainApp.showErrorDialog("CLIENT - unhandled error in GUI", throwable);
foundCount++;
}
foundErrors.put(errorKey, foundCount++);
} catch (Throwable ignore) {
}
}
public static void registerExceptionHandler() {
@ -29,4 +45,8 @@ public class EDTExceptionHandler implements Thread.UncaughtExceptionHandler {
System.setProperty("sun.awt.exception.handler", EDTExceptionHandler.class.getName());
}
public static void registerMainApp(MageFrame app) {
mainApp = app;
}
}