2

I have a custom Interceptor, from which I throw an Exception;

The Action(s) running that Interceptor is managed by Convention plugin;

The Exception raised by the Interceptor is globally defined in struts.xml for the package the Action is running into.

RESULT: the exception mapping is ignored and I get the

Struts Problem Report

Struts has detected an unhandled exception:

...

Stacktraces

java.lang.IllegalArgumentException: my message

I guess I'm just missing something stupid... we've already discussed of this in a similar question, but it's still not clear if it can or can't work this way:

struts.xml

<package name="my-package" namespace="my" extends="struts-default">
    <interceptors>

        <interceptor name="myInterceptor" class="foo.bar.MyInterceptor"/>

        <interceptor-stack name="myStack">
            <interceptor-ref name="myInterceptor"/>
            <interceptor-ref name="defaultStack"/>
        </interceptor-stack>

    </interceptors> 

    <default-interceptor-ref name="myStack"/>

    <global-results>
        <result name="input">/WEB-INF/content/my-input.jsp</result>
        <result name="error">/WEB-INF/content/my-error.jsp</result>
    </global-results>

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

Action

@ParentPackage("my-package")
@Namespace("/my/blabla/yadayada")
public class MyAction extends MyBaseAction {

}

Interceptor

@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
    // ....
    if (somethingWrong) {
        throw new IllegalArgumentException("All work and no play makes Jack a dull boy");
    }
}

I've also tried defining global result / global exception mapping in the abstract BaseAction, or in the physical real Action itself, but they're ignored too.

Any idea on what to add / remove / change in order to make it work as it should ? This is not esoteric stuff, this is basic :|

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • 2.3.16.3 (Can't update to 2.3.20 due to incompatibilities with struts2-jquery plugin 3.7.1... waiting for 4.0.0 then) – Andrea Ligios Dec 23 '14 at 16:26
  • So there isn't *any* way to map an exception returned by an interceptor to a known result ? I can return a result from the Interceptor, but it would be not decoupled from the project configuration anymore... if I return an exception it can be mapped or not (and hence be displayed beautified or raw), while if I return a result it should be defined in the config... BTW add it as an answer so I can upvote it and (most likely) accept – Andrea Ligios Dec 23 '14 at 16:33
  • Off-topic: *...incompatibilities with struts2-jquery plugin 3.7.1* Do you know any of them? I just upgraded an app with some of s2-jqr functionality and it seems working fine... for now. ;) – Aleksandr M Dec 23 '14 at 16:37
  • Try to put your interceptor after the `exception` interceptor in the interceptor stack. – Aleksandr M Dec 23 '14 at 16:44
  • 1
    It crossed my mind but I haven't tried... I'm doing it now. – Andrea Ligios Dec 23 '14 at 17:09
  • About the 2.3.20 + 3.7.1 problems, I've just tried upgrading the first one the day after he went out, and I've got several incompatibilities (I remember with sruts jquery plugin, but maybe there were others...). I was not able to deploy the project and make it work, so I reverted and put myself in WAIT for it while I'm rushing to close projects by the end of the year. I'll retry ASAP... it was probably a project or ecosystem (Jboss EAP 6) related problem, if you say you can deploy and use them with no problem. – Andrea Ligios Dec 23 '14 at 17:26
  • 1
    I don't use s2-jqr plugin much so I cannot say for sure that all features are working, but datepickers and dialogs were fine. **Also regarding update to 2.3.20 and this question**, see https://issues.apache.org/jira/browse/WW-4433. Good luck with update. – Aleksandr M Dec 23 '14 at 17:38
  • Nice :D BTW no, I wasn't even able to deploy IIRC, so mine was another problem. I rememeber vaguely about not found/loaded classes during the bootstrap, but I've seen too many stuff in this two weeks and I had like max 20 minutes for the migration, so I happily decided to wait :) – Andrea Ligios Dec 23 '14 at 17:44

1 Answers1

3

The main candidate for exception mapping feature is actions throwing an exceptions.

Docs:

Exception mappings is a powerful feature for dealing with an Action class that throws an Exception. The core idea is that an Exception thrown during the Action method can be automatically caught and mapped to a predefined Result.

But exceptions thrown from interceptors can be also handled by exception interceptor. In order to catch other interceptors exceptions exception interceptor should be defined as the first interceptor in the stack.

From the ExceptionMappingInterceptor javadoc:

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.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143