This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-4.8.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.8.x by this push: new e53a2e6c800 CAMEL-21954: camel-core: Fix onException not working if route was using nodePrefixId (used in route templates) e53a2e6c800 is described below commit e53a2e6c8002a41114da061ce384893a4b1dc040 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Fri Apr 11 12:15:40 2025 +0200 CAMEL-21954: camel-core: Fix onException not working if route was using nodePrefixId (used in route templates) --- .../apache/camel/reifier/OnExceptionReifier.java | 5 ++ .../builder/RoutePrefixIdOnExceptionTest.java | 52 ++++++++++++++++ .../builder/RouteTemplateOnExceptionTest.java | 70 ++++++++++++++++++++++ 3 files changed, 127 insertions(+) diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/OnExceptionReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/OnExceptionReifier.java index ec20b975554..3009d419f66 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/OnExceptionReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/OnExceptionReifier.java @@ -51,7 +51,12 @@ public class OnExceptionReifier extends ProcessorReifier<OnExceptionDefinition> // wrap in our special safe fallback error handler if OnException // have child output Processor errorHandler = new FatalFallbackErrorHandler(child, false); + // clip node prefix id from the onException id we stored on the route String id = getId(definition); + String prefix = definition.getNodePrefixId(); + if (prefix != null && id.startsWith(prefix)) { + id = id.substring(prefix.length()); + } route.setOnException(id, errorHandler); } // lookup the error handler builder diff --git a/core/camel-core/src/test/java/org/apache/camel/builder/RoutePrefixIdOnExceptionTest.java b/core/camel-core/src/test/java/org/apache/camel/builder/RoutePrefixIdOnExceptionTest.java new file mode 100644 index 00000000000..746a61566b3 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/builder/RoutePrefixIdOnExceptionTest.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.builder; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class RoutePrefixIdOnExceptionTest extends ContextTestSupport { + + @Test + public void testOnException() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(0); + getMockEndpoint("mock:error").expectedMessageCount(1); + + Exchange out = fluentTemplate.withBody("Hello World").to("direct:start").send(); + Assertions.assertTrue(out.isRollbackOnly()); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").nodePrefixId("myPrefix") + .onException(Exception.class) + .to("mock:error") + .markRollbackOnly() + .end() + .throwException(new IllegalArgumentException("Forced")) + .to("mock:result"); + } + }; + } +} diff --git a/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateOnExceptionTest.java b/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateOnExceptionTest.java new file mode 100644 index 00000000000..afd5a73694b --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateOnExceptionTest.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.builder; + +import java.util.Map; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class RouteTemplateOnExceptionTest extends ContextTestSupport { + + @Test + public void testOnException() throws Exception { + context.addRouteFromTemplate("myRoute", "myTemplate", Map.of("foo", "start")); + + getMockEndpoint("mock:result").expectedMessageCount(0); + getMockEndpoint("mock:error").expectedMessageCount(1); + + Exchange out = fluentTemplate.withBody("Hello World").to("direct:start").send(); + Assertions.assertTrue(out.isRollbackOnly()); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testOnExceptionPrefixId() throws Exception { + context.addRouteFromTemplate("myRoute", "myTemplate", "myPrefix", Map.of("foo", "start")); + + getMockEndpoint("mock:result").expectedMessageCount(0); + getMockEndpoint("mock:error").expectedMessageCount(1); + + Exchange out = fluentTemplate.withBody("Bye World").to("direct:start").send(); + Assertions.assertTrue(out.isRollbackOnly()); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + routeTemplate("myTemplate").templateParameter("foo") + .from("direct:{{foo}}") + .onException(Exception.class) + .to("mock:error") + .markRollbackOnly() + .end() + .throwException(new IllegalArgumentException("Forced")) + .to("mock:result"); + } + }; + } +}