Lukas Grygar's

Personal enterprise Java web development blog.

Spring Web Flow: Exception does not get logged while using 'transition on-exception'

While using Spring Web Flow you may encounter problem that exception which gets caught at ‘transition on-exception’ doesn’t get logged.

Problem

Try to put following code into your handler method:

throw new RuntimeException();

and with following configuration in your *-flow.xml

<view-state id="errorView" view="error/generalError" />

<global-transitions>
    <transition on-exception="java.lang.Exception" to="errorView" />
</global-transitions>

exception won’t get logged.

Solution

You could fix it with following logFlowException method bellow:

package com.lukasgrygar.testapp.util;

import org.apache.log4j.Logger;

public class Util {

    private static Logger log = Logger.getLogger(Util.class);

   /**
     * Used to log exceptions handled by global transitions on-exception handler.
     * These exceptions aren't propagated either to FlowHandlerAdapter.handleException nor to AbstractFlowHandler.handleException.
     * 
     * @param rootCauseException
     * @param stateException
     */
    public void logFlowException(Exception rootCauseException, Exception stateException) {
       if (rootCauseException != null) {
            log.error("logFlowException(): Unexpected flow exception.", rootCauseException);
        } else if (stateException != null) {
            log.error("logFlowException(): Unexpected flow exception.", stateException);
        } else {
            log.error("logFlowException(): Unexpected flow exception, but exception detail is missing. This shouldn't happen.");
        }
    }
}

And then you need to modify your *-flow.xml configuration file, to evaluate logFlowException method everytime an Exception occurs:

<view-state id="errorView" view="error/generalError" />

<global-transitions>
    <transition on-exception="java.lang.Exception" to="errorView">
        <evaluate expression="util.logFlowException(flashScope.rootCauseException, flashScope.stateException)" />
    </transition>
</global-transitions>

and don’t forget to add bean configuration to your context configuration file:

<bean id="util" class="com.lukasgrygar.testapp.util.Util" />

I’m using Spring Web Flow version 2.3.3.RELEASE.

I would like to give a credit to my senior colleague for this solution.


Tags: java  spring  spring-web-flow  spring-webflow 

Share on: Twitter  Facebook  Google+ 


Previous post: Liferay: How to encrypt (hash) password

Next post: Spring REST service: GET path variable gets truncated after dot '.' and HTTP Error 406 'Not acceptable' solution

Related posts: