SAS 9.1.3 Integration Technologies » SAS Web Infrastructure Kit: Developer's Guide


Developing Custom Portlets
Development Steps
Creating a Deployment Descriptor
Creating Display Resources Files
Developing the Presentation JSP Page
Creating Action Classes
Implementing Portlet Help
Creating a PAR File
Use Cases
Simple Display Portlet
Localized Portlet
Portlet Template (Editable Portlet)
Remote Portlet
Tips and Best Practices
Using the Portlet API
Sample Portlets
Localized Display Portlet (Welcome)
Interactive Form Portlet (FormExample)
Portlet Template, or Editable Portlet (DisplayURL)
Web Application (HelloUserWikExample)
Remote Portlet (HelloUserRemote
Portlet
Development Steps: Creating Action Classes

Creating an Error Handling Action

The com.sas.portal.portlet.ErrorHandlerInterface is available for gracefully handling any errors that your local portlets encounter. This interface has one method, which is called service(). This method has the same arguments as the service() method of the PortletActionInterface, plus an additional object called Exception.

If you specify an error handler in your portlet deployment descriptor file (portlet.xml), the error handler will be called if the portlet action throws an exception. You can direct your error handler to send messages to the server log and to return a URL string representing an error page for the user to view.

If your portlet initializer encounters an exception, the error handler will not be called. If you want to ensure that the error handler will execute, you can store the exception object in the portlet context. Then, in your action class's service() method, you can get the exception object out of the context and re-throw it. In the following example, this code is put into a method that should be called at the start of the action's service() method:

/**Check the PortletContext for an exception object. If 
 * present, throw it so that the error handler will kick in.
 * @param context the PortletContext
 */
  private static void errorCheck(PortletContext context)
  throws Exception {
Exception e = (Exception) context.getAttribute("PORTLET_EXCEPTION");
    if (e != null)
    {
      throw e;
    }
  }

Here is a simple error handler that logs the exception and calls a static error page. The error page supplies a general error message from the portlet's localized resource bundles.

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sas.portal.portlet.PortletContext;
import com.sas.services.logging.LoggingServiceInterface;
import com.sas.portal.portlet.ErrorHandlerInterface;
import com.sas.portal.portlet.NavigationUtil;

/**Error handler for some portlets.
 * It logs the exception and returns ErrorPage.jsp 
 * for the portlet to display.
 */
public class MyErrorHandler implements ErrorHandlerInterface {

  private final String _loggingContext = this.getClass().getName();

  /**Returns the URL for the portlet controller to call. This is the
   * name of the error page JSP.
   * @param request the HttpServletRequest
   * @param response the HttpServeltResponse
   * @param context the PortletContext
   * @param exception the exception thrown by a portlet action
   * @return the URL to call
   */
public String service(HttpServletRequest request, HttpServletResponse 
 response, PortletContext context, Exception thrownException)
  {
  [code omitted for obtaining a LoggerInterface]
    //prepare the localized resources for use by the jsp.
    try {
        NavigationUtil.prepareLocalizedResources(
          "com.mycompany.portlets.Resources", request, context);
    }
    catch (java.io.IOException ioe) {
      logger.error(ioe.getMessage(), _loggingContext, ioe);
    }

    logger.error(thrownException.getMessage(), _loggingContext,
                 thrownException);
    return "ErrorPage.jsp";
  }
}