Error Handling

YASL's error handling classes provide a mechanism for displaying runtime error information in a user-friendly manner. Developers are encouraged to implement their own error handlers to address the unique needs of their applications.


Error Handler Interface

Any class that implements the YASLErrorHandler interface (from org.yasl.arch.errors) can act as an error handler. Presently, the interface has one method: public void handleAppError(Exception ex).


Default Implementation

The default implementation of the error handler interface is YASLErrorHandlerImpl (from org.yasl.arch.impl.errors). If you fail to configure your own error handler, this default version will be used. The implementation for the handleAppError() method is listed below:

private final YASLApplicationType appType; // in ctor

/**
 * Creates an instance of the default error handler.
 *
 * @param appType YASLApplicationType
 */
public YASLErrorHandlerImpl(YASLApplicationType appType) {
  this.appType = appType;
}

/**
 * Provides a mechanism to log and/or display error information to the
 * user.
 *
 * For comandline applications, the stacktrace is sent to standard error.
 *
 * For Applets and Swing applications, the message associated with the
 * exception is displayed in a message dialog and the stacktrace is sent
 * to standard error.
 *
 * @param ex Exception
 */
public void handleAppError(Exception ex) {
  logger.error("Encountered app error", ex);
  String message = ex.getLocalizedMessage();
  if (message == null) {
    message = ex.toString();
  }
  if (appType == YASLApplicationType.CMMNDLINE_APP) {
    System.err.println(message);
  }
  else { // applet or swing app
    // passing null for parent, will get root frame
    JOptionPane.showMessageDialog(null,
                                  message,
                                  "Application Error",
                                  JOptionPane.ERROR_MESSAGE);
  }
  // send stacktrace to standard error
  ex.printStackTrace();
}


Storing and Accessing the Error Handler

The preferred method for accessing the error handler is through the YASLApplication interface method getErrorHandler(). The error handler is stored in the singletons map using the standard error handler key defined in the YASLApplication interface. If an application implments its own error handler, it must store the error handler in the singletons map using the standard key. This can be done during XML configuration or programatically by calling the setSingleton() method from the YASLApplication interface.

From YASLApplication:

public static final String KEY_ERROR_HANDLER = "yasl_error_handler";

public YASLErrorHandler getErrorHandler();


Error Handler Usage: Example

The example below is taken from YASLGUIAbstractAction.

public final void actionPerformed(ActionEvent e) {
  final ActionEvent event = e;
  Runnable runner = new Runnable() {
    public void run() {
      try {
        performAction(event);
      }
      catch (Exception ex) {
        yaslApplication.getErrorHandler().handleAppError(ex);
      }
    }
  };
  SwingUtilities.invokeLater(runner);
}