How pass body variable as a parameter to custom Exception

668 views Asked by At

I'm struggling to understand how passing a variable stored in the body as a parameter of throwException. This is my code:

.when(simple("${body[errorCode]} contains '101'"))
.throwException(new IllegalArgumentException(
"Action not allowed- Error code:" + ${body[errorCode]))
 .otherwise()

When I run the application the message passed to ErrorHandling is

'Action not allowed- Error code:${body[errorCode]', no replacing for errorCode variable.

Any suggestions? Tnks.

3

There are 3 answers

2
Namphibian On

So you are using the Simple language in Java but you have some syntax issues. Nothing major. Your do not complete the delimiter of the Simple expression. Also you dont have to concatenate the string.

Change the code:

.throwException(new IllegalArgumentException("Action not allowed- Error code:" + ${body[errorCode]))

To: .throwException(new IllegalArgumentException("Action not allowed- Error code: ${body[errorCode]}"))

I am on the buss and using my phone so can't check if the code runs but it should point you in the right direction.

1
Claus Ibsen On
0
Francisco On
@Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start")
                        .to("mock:start")
                        **.throwException(IllegalArgumentException.class, "Darn ${body} is invalid")**
                        .to("mock:result");
            }
        };
    }