You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Minh Tran <da...@gmail.com> on 2016/12/18 23:54:28 UTC

Exception handling with causes and onWhen

Hi

I want to be able to handle an exception that has particular cause exception with a particular message. I cannot see how I can do this in a very good way. Here’s an example

I have a RuntimeException with message “outer exception" with a cause of type Exception with message “inner exception”

My exception handling definition looks like this

	 <onException>
            <exception>java.lang.Exception</exception>
            <onWhen>
                <simple>${exception.message} contains ‘inner'</simple>
            </onWhen>
        </onException>

My unit test shows that my exception isn’t being captured. However if I use

	  <onWhen>
                <simple>${exception.cause.message} contains ‘inner'</simple>
            </onWhen>

This works. And the following also works
	  <onWhen>
                <simple>${exception.message} contains ‘outer'</simple>
            </onWhen>


I suspect this is because the $exception property is always set to the top level exception. I find this very confusing as I’m sure when you read the above, you would expect the exception.message to be from Exception, not RuntimeException.

So my question is, what is the best way to capture any cause exceptions with a particular message, even if it is many layers deep? I don’t want to resort to something like ${exception.cause.cause.cause.message}

I am using Camel 2.18.1.