Handling errors and exceptions
In this topic ...
Catch and handle an error in a model
Catch all un-handled errors in a model
Display a custom error page for un-handled errors
Related Topics...
There are several ways we can handle errors and exceptions generated by an application. The Factory contains a special Builder -- Error Handler -- we can use to catch and handle exceptions in a model. In addition we can use the Java language s Try/Catch error handling mechanisms to handle exceptions generated by Methods and Linked Java Objects included in your application.
Catch and handle an error in a model
To catch and handle an exception (error) in a model or an action, place an Error Handler Builder in the model and assign it to an action. When you place this builder in a model, you specify:
- Try Action - This is the action you want the Builder to monitor. For example, such an action might be the loading of a particular page.
- Catch Action - This is the action you want the Builder to initiate if an exception occurs. For example, you might want to invoke a method or display a specific error-related page.
We can place more than one Error Handler Builder in a model, and we can monitor exceptions at various levels. For example, we can assign as the "Try Action" a Page Action, a Method Action, or an individual action in an Action List.
To work with a specific type of exception in a method, we can specify an "Exception Class" in the Error Handler Builder. (The indirect reference widget for this builder input allows you to select a Java class in your project.) This technique allows you to assign multiple error handler builders to multiple exceptions on the same method or model.
Review How to Use the Handler Builder to learn more about using this builder.
Catch all un-handled errors in a model
Place an Error Handler Builder in the model and enable the "Catch all Errors" radio button. The Builder will catch and handle any unhandled errors encountered when the model is run.
Display a custom error page for un-handled errors
It s easy to display your own error page when an error that is not handled by an Error Handler Builder occurs in a model. All we need to do is:
- Create an alternate JSP error page for display.
Tip - Copy the existing default error page and modify it as required. (This file is located at: factory/webapp/defaulterror.jsp)
- Change the Factory property that determines the error page is called when an un-handled error occurs.
An un-handled error is automatically passed to a JSP error page and this page is displayed to the user. We can override the default page provided and substitute your own error page by changing the Factory property that specifies the page to be displayed. You will add this property to override.properties file located in a project s /WEB-INF/config directory. The comment and property entry we need to make in the override.properties file is:
# Default error page for unhandled exceptions.
bowstreet.webapp.errorHandler=/factory/webapp/defaulterror.jsp
Simply replace the page defaulterror.jsp with the name of your JSP page.
Handle an error in Java code
We can handle errors in LJO methods using the standard try/catch mechanism. In the "catch" code, you extract the error information that the Factory servlet stores in the request. Once you process the error information, we can perform one or more actions to allow processing to proceed or we can simply return an error page.
For example, if the method tries to open a file and fails because the file does not exist, the "catch" actions could store the error in a variable to be used to supply an error message and display a page in which the user can name another file to open. Unhandled, a "file not found" error results in the application stopping and the display of the default unhandled error page.
The following code sample shows a method that tries to open a file, and if it is not found, stores the error message in a variable and displays a "Specify New File" page:
catch (IOException ioe) {
HttpServletRequest request = webAppAccess.getHttpServletRequest();
Throwable ex = (Throwable)request.getAttribute("bowstreet.errorhandler.Exception");
String actionName = (String)request.getAttribute("bowstreet.errorhandler.ActionName");
String errorMessage = ex.toString() + " occurred in: " + actionName;
//log the error
webAppAccess.logError(actionName, ex);
//set a the value of a variable to the error message
webAppAccess.getVariables().setString("ErrorVariable", errorMessage);
//display a page prompting for new information
webAppAccess.processPage("ErrorPage");
}
Write a Java error to the log
We can write the above error to the log with the logError() method on the webAppAccess object. The logError() method takes a string and a Throwable object as shown below:
//log the error
webAppAccess.logError(actionName, ex);
Where actionName is the value of the bowstreet.errorhandler.ActionName attribute in the request and ex is the value of the bowstreet.errorhandler.Exception attribute in the request.
Review Log4j System Level Logging to learn more about the logging options available.