com.opensymphony.xwork.interceptor
Class ExceptionMappingInterceptor

java.lang.Object
  extended by com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor
All Implemented Interfaces:
Interceptor

public class ExceptionMappingInterceptor
extends Object
implements Interceptor

This interceptor forms the core functionality of the exception handling feature. Exception handling allows you to map an exception to a result code, just as if the action returned a result code instead of throwing an unexpected exception. When an exception is encountered, it is wrapped with an ExceptionHolder and pushed on the stack, providing easy access to the exception from within your result. Note: While you can configure exception mapping in your configuration file at any point, the configuration will not have any effect if this interceptor is not in the interceptor stack for your actions. It is recommended that you make this interceptor the first interceptor on the stack, ensuring that it has full access to catch any exception, even those caused by other interceptors.

Interceptor parameters:

Extending the interceptor:

If you want to add custom handling for publishing the Exception, you may override publishException(com.opensymphony.xwork.ActionInvocation, ExceptionHolder). The default implementation pushes the given ExceptionHolder on value stack. A custom implementation could add additional logging etc.

Example code:

 
 <xwork>
     <include file="webwork-default.xml"/>

     <package name="default" extends="webwork-default">
         <global-results>
             <result name="success" type="freemarker">error.ftl</result>
         </global-results>

         <global-exception-mappings>
             <exception-mapping exception="java.lang.Exception" result="error"/>
         </global-exception-mappings>

         <action name="test">
             <interceptor-ref name="exception"/>
             <interceptor-ref name="basicStack"/>
             <exception-mapping exception="com.acme.CustomException" result="custom_error"/>
             <result name="custom_error">custom_error.ftl</result>
             <result name="success" type="freemarker">test.ftl</result>
         </action>
     </package>
 </xwork>
 
 

Author:
Matthew E. Porter (matthew dot porter at metissian dot com) Date: Aug 14, 2005 Time: 12:40:02 PM

Field Summary
protected  org.apache.commons.logging.Log log
           
 
Constructor Summary
ExceptionMappingInterceptor()
           
 
Method Summary
 void destroy()
          Called to let an interceptor clean up any resources it has allocated.
 int getDepth(String exceptionMapping, Throwable t)
          Return the depth to the superclass matching.
 void init()
          Called after an interceptor is created, but before any requests are processed using intercept , giving the Interceptor a chance to initialize any needed resources.
 String intercept(ActionInvocation invocation)
          Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the request by the ActionInvocation or to short-circuit the processing and just return a String return code.
protected  void publishException(ActionInvocation invocation, ExceptionHolder exceptionHolder)
          Default implementation to handle ExceptionHolder publishing.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected org.apache.commons.logging.Log log
Constructor Detail

ExceptionMappingInterceptor

public ExceptionMappingInterceptor()
Method Detail

destroy

public void destroy()
Description copied from interface: Interceptor
Called to let an interceptor clean up any resources it has allocated.

Specified by:
destroy in interface Interceptor

init

public void init()
Description copied from interface: Interceptor
Called after an interceptor is created, but before any requests are processed using intercept , giving the Interceptor a chance to initialize any needed resources.

Specified by:
init in interface Interceptor

intercept

public String intercept(ActionInvocation invocation)
                 throws Exception
Description copied from interface: Interceptor
Allows the Interceptor to do some processing on the request before and/or after the rest of the processing of the request by the ActionInvocation or to short-circuit the processing and just return a String return code.

Specified by:
intercept in interface Interceptor
Returns:
the return code, either returned from ActionInvocation.invoke(), or from the interceptor itself.
Throws:
Exception - any system-level error, as defined in Action.execute().

getDepth

public int getDepth(String exceptionMapping,
                    Throwable t)
Return the depth to the superclass matching. 0 means ex matches exactly. Returns -1 if there's no match. Otherwise, returns depth. Lowest depth wins.


publishException

protected void publishException(ActionInvocation invocation,
                                ExceptionHolder exceptionHolder)
Default implementation to handle ExceptionHolder publishing. Pushes given ExceptionHolder on the stack. Subclasses may override this to customize publishing.

Parameters:
invocation - The invocation to publish Exception for.
exceptionHolder - The exceptionHolder wrapping the Exception to publish.

XWork Project Page