Programmers Guide > Error Handling

Error Handling

Default Error Handling:

A JOT Servlet handles an uncaught exception with the failureResponse() method. The exception is logged with the precise JOT Template line number and context information (see Logging and Debugging). Default error handling sends a response to the client with the ResponseException respond() method. The response includes a specified HTTP status code and a "friendly" client message if the exception implements ServletResponseException.

A catch() clause can also use the wrapResponseException() utility method. The following example logs an IllegalArgumentsException and sends the client message "Unable to process your request. Please try again" with an HTTP 400 status code (Bad Request):

    catch (IllegalArgumentsException iaex) {
        throw ResponseException.wrapResponseException(iaex, 400,
            "Unable to process your request. Please try again", false);
    }

Customized Error Handling:

A JotHttpServlet subclass can override exceptionResponse() and render a JOT Component error page with context sensitive content, as in this example:

    public void exceptionResponse(
        JotServletServiceContext serviceContext,
        JotServletException jsex)
            throws JotServletException
    {
        serviceContext.log(jsex.getJotException());
        JotViews.serviceRequest(context, MyErrorHandlerJotResponseBean.class);
    }

An example of this approach is the PickaNumServlet exceptionResponse() method.

JotException Request Attribute:

The JOT Views rendering engine throws JotServletException. JOT JSP tags throw JotJspException. These events save the JotException object in the JOT_SERVLET_EXCEPTION Request attribute. The JotException can be accessed by an error handler, as in this example:

    JotException jex = (JotException)
        getRequest().getAttribute(JotServletException.JOT_SERVLET_EXCEPTION);

Since Request attributes are automatic properties, the exception can also be used on a JOT Template as shown in the following example that tests for the ClientMessage property:

    JOT.JOT_SERVLET_EXCEPTION.IfProperty(ClientMessage,"System is totally hosed")
© 2007 JOT Object Technologies