minie
minie

Reputation: 21

How to handle an exception in an exception block in apache camel

I am trying to handle an exception within apache camel in onException. Can someone guide me if it is possible?

I have written another onException which will handle all Exceptions, but the flow is not transferred to that exception block

onException(SchemaValidationException.class)
        .to("xslt:stylesheet/example/TransformErrorBlock.xsl?saxon=true")
        .log("Validation error in received message, response sent: ${body}")
        .handled(true);

My expectation is if there is an exception in this block, it should be caught in another onException block

Upvotes: 1

Views: 1389

Answers (2)

Rajveer Singh
Rajveer Singh

Reputation: 13

This worked for me:

onException(SalesforceException.class)
    .handled(true)
    .log("mai hua")
    .process((exchange) -> {
        Exception e = new Exception("Some Exception");
        exchange.setProperty("CustomException", e);
    })
    .to("direct:exception");


onException(Exception.class)
    .handled(true)
    .log("mai bhi hua2");

from("direct:exception")
    .log("mai bhi hua in route")
    .process(exchange -> {
        throw exchange.getProperty("CustomException", Exception.class);
    });

Upvotes: 0

Claus Ibsen
Claus Ibsen

Reputation: 55540

You cannot do this as its by design that Camel only allows onException block to handle exceptions, otherwise you can end up with endless looping when onException A is handled by onException which causes a new exception that may then be handled by onException A again, and so endless looping in circles.

Upvotes: 2

Related Questions