public class ExceptionMappingInterceptor extends AbstractInterceptor
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:
trace, debug, info, warn, error, fatal
)? - defaut is debug
com.mycompany.app
).
Default is to use com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor
.publishException(com.opensymphony.xwork2.ActionInvocation, ExceptionHolder)
. The default implementation
pushes the given ExceptionHolder on value stack. A custom implementation could add additional logging etc.
Example code:
<xwork> <package name="default" extends="xwork-default"> <global-results> <result name="error" 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>This second example will also log the exceptions using our own category
com.mycompany.app.unhandled at WARN level.
<xwork>
<package name="something" extends="xwork-default">
<interceptors>
<interceptor-stack name="exceptionmappingStack">
<interceptor-ref name="exception">
<param name="logEnabled">true</param>
<param name="logCategory">com.mycompany.app.unhandled</param>
<param name="logLevel">WARN</param>
</interceptor-ref>
<interceptor-ref name="i18n"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="exceptionmappingStack"/>
<global-results>
<result name="unhandledException">/unhandled-exception.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="unhandledException"/>
</global-exception-mappings>
<action name="exceptionDemo" class="org.apache.struts2.showcase.exceptionmapping.ExceptionMappingAction">
<exception-mapping exception="org.apache.struts2.showcase.exceptionmapping.ExceptionMappingException"
result="damm"/>
<result name="input">index.jsp</result>
<result name="success">success.jsp</result>
<result name="damm">damm.jsp</result>
</action>
</package>
</xwork>
Modifier and Type | Field and Description |
---|---|
protected Logger |
categoryLogger |
protected static Logger |
LOG |
protected String |
logCategory |
protected boolean |
logEnabled |
protected String |
logLevel |
Constructor and Description |
---|
ExceptionMappingInterceptor() |
Modifier and Type | Method and Description |
---|---|
protected void |
doLog(Logger logger,
Exception e)
Performs the actual logging.
|
protected ExceptionMappingConfig |
findMappingFromExceptions(List<ExceptionMappingConfig> exceptionMappings,
Throwable t)
Try to find appropriate
ExceptionMappingConfig based on provided Throwable |
protected String |
findResultFromExceptions(List<ExceptionMappingConfig> exceptionMappings,
Throwable t)
Deprecated.
since 2.3.15 please use #findMappingFromExceptions directly instead
|
int |
getDepth(String exceptionMapping,
Throwable t)
Return the depth to the superclass matching.
|
String |
getLogCategory() |
String |
getLogLevel() |
protected void |
handleLogging(Exception e)
Handles the logging of the exception.
|
String |
intercept(ActionInvocation invocation)
Override to handle interception
|
boolean |
isLogEnabled() |
protected void |
publishException(ActionInvocation invocation,
ExceptionHolder exceptionHolder)
Default implementation to handle ExceptionHolder publishing.
|
void |
setLogCategory(String logCatgory) |
void |
setLogEnabled(boolean logEnabled) |
void |
setLogLevel(String logLevel) |
destroy, init
protected static final Logger LOG
protected Logger categoryLogger
protected boolean logEnabled
protected String logCategory
protected String logLevel
public boolean isLogEnabled()
public void setLogEnabled(boolean logEnabled)
public String getLogCategory()
public void setLogCategory(String logCatgory)
public String getLogLevel()
public void setLogLevel(String logLevel)
public String intercept(ActionInvocation invocation) throws Exception
AbstractInterceptor
intercept
in interface Interceptor
intercept
in class AbstractInterceptor
invocation
- the action invocationActionInvocation.invoke()
, or from the interceptor itself.Exception
- any system-level error, as defined in Action.execute()
.protected void handleLogging(Exception e)
e
- the exception to log.protected void doLog(Logger logger, Exception e)
logger
- the provided logger to use.e
- the exception to log.protected String findResultFromExceptions(List<ExceptionMappingConfig> exceptionMappings, Throwable t)
protected ExceptionMappingConfig findMappingFromExceptions(List<ExceptionMappingConfig> exceptionMappings, Throwable t)
ExceptionMappingConfig
based on provided ThrowableexceptionMappings
- list of defined exception mappingst
- caught exceptionpublic int getDepth(String exceptionMapping, Throwable t)
exceptionMapping
- the mapping classnamet
- the causeprotected void publishException(ActionInvocation invocation, ExceptionHolder exceptionHolder)
invocation
- The invocation to publish Exception for.exceptionHolder
- The exceptionHolder wrapping the Exception to publish.Copyright © 2000-2015 Apache Software Foundation. All Rights Reserved.