Apache Camel Route always return 404 even with exception handling

45 Views Asked by At

I've got Camel based app (Camel 2.25.4) and I have a route as follows:

rest("/example/")
              ... 
                .put("putExample").to("direct:putExample");
...

from("direct:putExample")
                .onException(HttpOperationFailedException.class)
                .handled(true)
                .process(new Processor()
                {
                    @Override
                    public void process(Exchange exchange) throws Exception
                    {
                        HttpOperationFailedException exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, HttpOperationFailedException.class);
                        if (exception.getStatusCode() == 404)
                        {
                            // Log, set a default message, or perform other actions
                            // For example, set a default body
                            exchange.getIn().setBody("Default response for 404");
                        }
                        // You could set a flag or header to indicate this exchange was handled due to a 404
                        exchange.getIn().setHeader("404Handled", true);
                    }
                })
                .end()
                .setHeader(Constants.HEADER_EMAIL, simple("${body[email]}"))
                .to("direct:getSomeDataAndReturn404")
                .choice()
                .when(header("404Handled"))
                // Process the exchange differently if it was handled due to a 404
                .log("Handled a 404 error. Continuing with custom logic.")
                .process(exchange ->
                {
                    System.out.println("Test");
                })
                .otherwise()
                // Continue normal processing if no 404 was encountered
                .log("No 404 error. Continuing normal route.")
                .process(exchange ->
                {
                    System.out.println("Test");
                })
                .endChoice();

"direct:getSomeDataAndReturn404" may return 404 if email won't be found.

My problem is, that I cannot "catch" this 404 and continue processing (I would like to execute another route, no matter if there will be 404 or not). However, no matter what I'm I doing, this route always returns 404, without going in to any of the processors.

If the email is found and there is 200 returned, everything is working fine.

I've also tried "doTry()" but effect was the same.

1

There are 1 best solutions below

0
On BEST ANSWER

TL;DR;

Instead of "to()", I should use ".enrich()". More about "enrich()" here.

Long Version

I think my problem was, that I've treaded routes as a methods and I was expecting that "to()" is just evaluation of the method that return something. If I'm not mistaken, that is a wrong assumption. When you use "to()" is more like you redirect flow, and then the target is "in charge" of it. So it can, for example, finish it (which I belive was a problem in my case). There are some ways to prevent this (like, for example, use .onException(Exception.class).continued(true)) but that's a different approach. However, using ".enrich()" and a custom aggregation strategy (example is in docs in TLDR of this answer)