You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Victor (JIRA)" <ji...@apache.org> on 2017/09/19 09:58:00 UTC

[jira] [Created] (CAMEL-11787) Try/catch on fault, not on exceptions

Victor created CAMEL-11787:
------------------------------

             Summary: Try/catch on fault, not on exceptions
                 Key: CAMEL-11787
                 URL: https://issues.apache.org/jira/browse/CAMEL-11787
             Project: Camel
          Issue Type: New Feature
          Components: camel-core
            Reporter: Victor


Hi,



I would like to be able to catch exchange that are faults in a try/catch.

Something like this should work:
{code:java}
public class TrySetFaultTest extends CamelTestSupport {

    @Test
    public void testSetFault() throws Exception {
        getMockEndpoint("mock:a").expectedMessageCount(0);
        getMockEndpoint("mock:catch-a").expectedMessageCount(1);

        template.requestBody("direct:start", "Hello World");

        assertMockEndpointsSatisfied(1, TimeUnit.SECONDS);
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                context.setTracing(true);

                from("direct:start")
                    .doTry()
                        .setFaultBody(constant("Failed at A"))
                        .to("mock:a")
                    .doCatch(Exception.class) // of course, this does not!
                        .to("mock:catch-a")
                    .end();
            }
        };
    }
}
{code}

The following existing test: https://github.com/apache/camel/blob/master/camel-core/src/test/java/org/apache/camel/processor/TrySetFaultTest.java is incorrect in my opinion: it lets people think that it should work, but the exchange never reach "mock:catch-a"!

There seems to be no way to express what I want (without going through complex stuffs), hence this feature request.

An alternative to this would be to be able to tell camel not to stop processing when there is a fault, so that I can simply handle it at the level of "mock:a":
{code:java}
public class TrySetFaultTest extends CamelTestSupport {

    @Test
    public void testSetFault() throws Exception {
        getMockEndpoint("mock:a").expectedMessageCount(1);

        template.requestBody("direct:start", "Hello World");

        assertMockEndpointsSatisfied(1, TimeUnit.SECONDS);
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                context.setTracing(true);

                from("direct:start")
                    // here something to tell camel not to stop on faults!
                    .setFaultBody(constant("Failed at A"))
                    .to("mock:a");
            }
        };
    }
}
{code}

Thanks!



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)