1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
package exceptions;
import javax.swing.JOptionPane;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Centralized error handling utility for the DS-Sim application.
* Provides consistent error logging and user notification.
*
* @author Paul C. Buetow
*/
public final class VSErrorHandler {
private static final Logger LOGGER = Logger.getLogger(VSErrorHandler.class.getName());
/** Private constructor to prevent instantiation */
private VSErrorHandler() {}
/**
* Handle an exception with logging and optional user notification.
*
* @param exception the exception to handle
* @param showDialog whether to show a dialog to the user
*/
public static void handle(Exception exception, boolean showDialog) {
// Log the exception
LOGGER.log(Level.SEVERE, exception.getMessage(), exception);
// Show dialog if requested
if (showDialog) {
showErrorDialog(exception);
}
}
/**
* Handle an exception with logging only (no user notification).
*
* @param exception the exception to handle
*/
public static void handle(Exception exception) {
handle(exception, false);
}
/**
* Handle an exception with a custom message.
*
* @param message custom message to log and display
* @param exception the exception to handle
* @param showDialog whether to show a dialog to the user
*/
public static void handle(String message, Exception exception, boolean showDialog) {
// Log with custom message
LOGGER.log(Level.SEVERE, message, exception);
// Show dialog if requested
if (showDialog) {
showErrorDialog(message, exception);
}
}
/**
* Log a warning without throwing an exception.
*
* @param message the warning message
*/
public static void warning(String message) {
LOGGER.log(Level.WARNING, message);
}
/**
* Log a warning with an associated exception.
*
* @param message the warning message
* @param exception the associated exception
*/
public static void warning(String message, Exception exception) {
LOGGER.log(Level.WARNING, message, exception);
}
/**
* Show an error dialog to the user.
*
* @param exception the exception to display
*/
private static void showErrorDialog(Exception exception) {
String title = switch (exception) {
case VSConfigurationException e -> "Configuration Error";
case VSProcessException e -> "Process Error";
case VSProtocolException e -> "Protocol Error";
case VSSerializationException e -> "Save/Load Error";
case VSSimulatorException e -> "Simulator Error";
default -> "Simulator Error";
};
String message = exception.getMessage();
if (message == null || message.isEmpty()) {
message = "An unexpected error occurred: " + exception.getClass().getSimpleName();
}
JOptionPane.showMessageDialog(null, message, title, JOptionPane.ERROR_MESSAGE);
}
/**
* Show an error dialog with a custom message.
*
* @param message the custom message
* @param exception the exception that occurred
*/
private static void showErrorDialog(String message, Exception exception) {
String details = exception.getMessage();
if (details != null && !details.isEmpty()) {
message = message + "\n\nDetails: " + details;
}
JOptionPane.showMessageDialog(null, message, "Simulator Error", JOptionPane.ERROR_MESSAGE);
}
/**
* Convert a generic exception to a simulator exception if possible.
*
* @param e the exception to convert
* @param context additional context about where the error occurred
* @return a VSSimulatorException wrapping the original exception
*/
public static VSSimulatorException wrap(Exception e, String context) {
return switch (e) {
case VSSimulatorException vse -> vse;
case java.io.IOException ioe -> new VSSerializationException(context, ioe);
case NumberFormatException nfe -> new VSConfigurationException("Invalid number format in " + context, nfe);
case NullPointerException npe -> new VSSimulatorException("Null value encountered in " + context, npe);
default -> new VSSimulatorException(context + ": " + e.getMessage(), e);
};
}
}
|