This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-4.10.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.10.x by this push:
new 15349027a7de CAMEL-22529: route configuration in Java DSL with
multiple onException does not work (#19778)
15349027a7de is described below
commit 15349027a7de047178e98fa0ca0d3f600a1fac4d
Author: Claus Ibsen <[email protected]>
AuthorDate: Sat Nov 1 11:53:29 2025 +0100
CAMEL-22529: route configuration in Java DSL with multiple onException does
not work (#19778)
---
.../apache/camel/model/OnExceptionDefinition.java | 4 +
.../apache/camel/model/ProcessorDefinition.java | 16 ++
.../model/RoutesConfigurationOnExceptionTest.java | 172 +++++++++++++++++++++
3 files changed, 192 insertions(+)
diff --git
a/core/camel-core-model/src/main/java/org/apache/camel/model/OnExceptionDefinition.java
b/core/camel-core-model/src/main/java/org/apache/camel/model/OnExceptionDefinition.java
index c4f398600768..b8fd7a16369f 100644
---
a/core/camel-core-model/src/main/java/org/apache/camel/model/OnExceptionDefinition.java
+++
b/core/camel-core-model/src/main/java/org/apache/camel/model/OnExceptionDefinition.java
@@ -207,6 +207,10 @@ public class OnExceptionDefinition extends
OutputDefinition<OnExceptionDefinitio
@Override
public OnExceptionDefinition onException(Class<? extends Throwable>
exceptionType) {
+ if (this.getRouteConfiguration() != null) {
+ // this is part of route configuration
+ return this.getRouteConfiguration().onException(exceptionType);
+ }
getExceptions().add(exceptionType.getName());
return this;
}
diff --git
a/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinition.java
b/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinition.java
index 2020877f452e..d3e02ca1f834 100644
---
a/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinition.java
+++
b/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinition.java
@@ -2256,6 +2256,10 @@ public abstract class ProcessorDefinition<Type extends
ProcessorDefinition<Type>
* @return the exception builder to configure
*/
public OnExceptionDefinition onException(Class<? extends Throwable>
exceptionType) {
+ if (this.getRouteConfiguration() != null) {
+ // this is part of route configuration
+ return this.getRouteConfiguration().onException(exceptionType);
+ }
OnExceptionDefinition answer = new
OnExceptionDefinition(exceptionType);
addOutput(answer);
return answer;
@@ -2271,6 +2275,10 @@ public abstract class ProcessorDefinition<Type extends
ProcessorDefinition<Type>
*/
public OnExceptionDefinition onException(
Class<? extends Throwable> exceptionType1, Class<? extends
Throwable> exceptionType2) {
+ if (this.getRouteConfiguration() != null) {
+ // this is part of route configuration
+ return this.getRouteConfiguration().onException(exceptionType1,
exceptionType2);
+ }
OnExceptionDefinition answer = new
OnExceptionDefinition(Arrays.asList(exceptionType1, exceptionType2));
addOutput(answer);
return answer;
@@ -2288,6 +2296,10 @@ public abstract class ProcessorDefinition<Type extends
ProcessorDefinition<Type>
public OnExceptionDefinition onException(
Class<? extends Throwable> exceptionType1, Class<? extends
Throwable> exceptionType2,
Class<? extends Throwable> exceptionType3) {
+ if (this.getRouteConfiguration() != null) {
+ // this is part of route configuration
+ return this.getRouteConfiguration().onException(exceptionType1,
exceptionType2, exceptionType2);
+ }
OnExceptionDefinition answer = new
OnExceptionDefinition(Arrays.asList(exceptionType1, exceptionType2,
exceptionType3));
addOutput(answer);
return answer;
@@ -2302,6 +2314,10 @@ public abstract class ProcessorDefinition<Type extends
ProcessorDefinition<Type>
*/
@SafeVarargs
public final OnExceptionDefinition onException(Class<? extends
Throwable>... exceptions) {
+ if (this.getRouteConfiguration() != null) {
+ // this is part of route configuration
+ return this.getRouteConfiguration().onException(exceptions);
+ }
OnExceptionDefinition answer = new
OnExceptionDefinition(Arrays.asList(exceptions));
addOutput(answer);
return answer;
diff --git
a/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationOnExceptionTest.java
b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationOnExceptionTest.java
new file mode 100644
index 000000000000..f4b8f653bd7e
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/model/RoutesConfigurationOnExceptionTest.java
@@ -0,0 +1,172 @@
+/*
+ * 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.model;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.RouteConfigurationBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class RoutesConfigurationOnExceptionTest extends ContextTestSupport {
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ @Test
+ public void testGlobal() throws Exception {
+ context.addRoutes(new RouteConfigurationBuilder() {
+ @Override
+ public void configuration() {
+ // global routes configuration
+ routeConfiguration()
+
.onException(IllegalArgumentException.class).handled(true).to("mock:error")
+
.onException(Exception.class).handled(true).to("mock:error2");
+
+ }
+ });
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:start")
+ .throwException(new IllegalArgumentException("Foo"));
+
+ from("direct:start2")
+ .throwException(new Exception("Foo2"));
+ }
+ });
+ context.start();
+
+ getMockEndpoint("mock:error").expectedBodiesReceived("Hello World");
+ getMockEndpoint("mock:error2").expectedBodiesReceived("Bye World");
+
+ template.sendBody("direct:start", "Hello World");
+ template.sendBody("direct:start2", "Bye World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testLocalOverride() throws Exception {
+ context.addRoutes(new RouteConfigurationBuilder() {
+ @Override
+ public void configuration() {
+ // global routes configuration
+ routeConfiguration()
+
.onException(IllegalArgumentException.class).handled(true).to("mock:error")
+
.onException(Exception.class).handled(true).to("mock:error2");
+
+ }
+ });
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:start")
+ .throwException(new IllegalArgumentException("Foo"));
+
+ from("direct:start2")
+
.onException(Exception.class).handled(true).to("mock:error3").end()
+ .throwException(new Exception("Foo2"));
+ }
+ });
+ context.start();
+
+ getMockEndpoint("mock:error").expectedBodiesReceived("Hello World");
+ getMockEndpoint("mock:error2").expectedMessageCount(0);
+ getMockEndpoint("mock:error3").expectedBodiesReceived("Bye World");
+
+ template.sendBody("direct:start", "Hello World");
+ template.sendBody("direct:start2", "Bye World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testLocalConfiguration() throws Exception {
+ context.addRoutes(new RouteConfigurationBuilder() {
+ @Override
+ public void configuration() {
+ routeConfiguration("mylocal")
+
.onException(IllegalArgumentException.class).handled(true).to("mock:error")
+
.onException(Exception.class).handled(true).to("mock:error2");
+
+ }
+ });
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:start")
+ .throwException(new IllegalArgumentException("Foo"));
+
+ from("direct:start2").routeConfigurationId("mylocal")
+ .throwException(new IllegalArgumentException("Foo2"));
+ }
+ });
+ context.start();
+
+ getMockEndpoint("mock:error").expectedBodiesReceived("Bye World");
+
+ assertThrows(Exception.class, () -> template.sendBody("direct:start",
"Hello World"),
+ "Should throw exception");
+
+ template.sendBody("direct:start2", "Bye World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Test
+ public void testGlobalAndLocal() throws Exception {
+ context.addRoutes(new RouteConfigurationBuilder() {
+ @Override
+ public void configuration() {
+ // global routes configuration
+ routeConfiguration()
+
.onException(IllegalArgumentException.class).handled(true).to("mock:error")
+
.onException(Exception.class).handled(true).to("mock:error2");
+
+ routeConfiguration("mylocal")
+
.onException(IllegalArgumentException.class).handled(true).to("mock:error3")
+
.onException(Exception.class).handled(true).to("mock:error4");
+ }
+ });
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:start")
+ .throwException(new IllegalArgumentException("Foo"));
+
+ from("direct:start2").routeConfigurationId("mylocal")
+ .throwException(new IllegalArgumentException("Foo2"));
+ }
+ });
+ context.start();
+
+ getMockEndpoint("mock:error").expectedBodiesReceived("Hello World");
+ getMockEndpoint("mock:error2").expectedMessageCount(0);
+ getMockEndpoint("mock:error3").expectedBodiesReceived("Bye World");
+ getMockEndpoint("mock:error4").expectedMessageCount(0);
+
+ template.sendBody("direct:start", "Hello World");
+ template.sendBody("direct:start2", "Bye World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+}