summaryrefslogtreecommitdiff
path: root/src/main/java/exceptions/VSErrorHandler.java
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-06-20 18:47:45 +0300
committerPaul Buetow <paul@buetow.org>2025-06-20 18:47:45 +0300
commit70fc0505b223f7bf17d3671d0532773359cf7858 (patch)
tree1c244371e6a4e89e7694d5691db6a14b0ba2da06 /src/main/java/exceptions/VSErrorHandler.java
parentf6d2a6bbbc37c552accf91a13ccd6ea45ecf8e73 (diff)
Implement proper exception hierarchy and consistent error handling
- Create base exception classes: - VSSimulatorException: Base checked exception for all simulator errors - VSSimulatorRuntimeException: Base unchecked exception for unrecoverable errors - Refactor existing exceptions to extend from base class: - VSEventNotCopyableException: Now includes better error messages - VSParseIntegerVectorException: Added constructors with cause support - VSNegativeNumberException: Added field name and value parameters - Add new specific exception types: - VSConfigurationException: For configuration errors - VSSerializationException: For save/load operations - VSProcessException: For process-related errors - VSProtocolException: For protocol execution errors - Create VSErrorHandler utility class: - Centralized error logging and user notification - Consistent error handling patterns - Helper methods for warning and error dialogs - Update exception handling in code: - VSClassLoader: Better error messages and specific exception handling - Timestamp events: Catch RuntimeException for custom actions - All 132 unit tests pass 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'src/main/java/exceptions/VSErrorHandler.java')
-rw-r--r--src/main/java/exceptions/VSErrorHandler.java148
1 files changed, 148 insertions, 0 deletions
diff --git a/src/main/java/exceptions/VSErrorHandler.java b/src/main/java/exceptions/VSErrorHandler.java
new file mode 100644
index 0000000..59085f0
--- /dev/null
+++ b/src/main/java/exceptions/VSErrorHandler.java
@@ -0,0 +1,148 @@
+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 = "Simulator Error";
+ String message = exception.getMessage();
+
+ if (exception instanceof VSSimulatorException) {
+ // Use more specific title for our exceptions
+ if (exception instanceof VSConfigurationException) {
+ title = "Configuration Error";
+ } else if (exception instanceof VSProcessException) {
+ title = "Process Error";
+ } else if (exception instanceof VSProtocolException) {
+ title = "Protocol Error";
+ } else if (exception instanceof VSSerializationException) {
+ title = "Save/Load Error";
+ }
+ }
+
+ 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) {
+ if (e instanceof VSSimulatorException) {
+ return (VSSimulatorException) e;
+ }
+
+ // Wrap common exceptions with more specific types
+ if (e instanceof java.io.IOException) {
+ return new VSSerializationException(context, e);
+ } else if (e instanceof NumberFormatException) {
+ return new VSConfigurationException("Invalid number format in " + context, e);
+ } else if (e instanceof NullPointerException) {
+ return new VSSimulatorException("Null value encountered in " + context, e);
+ } else {
+ return new VSSimulatorException(context + ": " + e.getMessage(), e);
+ }
+ }
+} \ No newline at end of file