Apache camel onException adding error details to the original message

Viewed 327

I have added this exception handling to the camel route.

 .onException(BeanCreationException.class, ValidationException.class)
     .handled(true)
     .process(new OnExceptionProcessor())
     .to("errorQueue0").id("errorQueue")
     .end()
public class OnExceptionProcessor implements Processor {
    @Override
    public void process(Exchange exchange) throws Exception {
        Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
        exchange.getIn().setHeader("FailedBecause", cause.getMessage());
    }
}

When I read this message back from the error queue, I cannot find this header. any idea on how to add error details along with the original message to the error queue

1 Answers

This could be a context problem because you are in a processor that is called by the error handler.

As an alternative, you could return the String value to set in the header from your processor method. By the way, this also improves the testability of your Processor.

Then you can use this return value to set the header in the error handler route directly.

.setHeader("FailedBecause", method(new OnExceptionProcessor()))
Related