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 9c81afadfdd8 CAMEL-22820: bridged error handler - Fix NPE if exception
on exchange is null (#20723)
9c81afadfdd8 is described below
commit 9c81afadfdd8f981ad5010697156ed497bb7d815
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Jan 8 20:26:59 2026 +0100
CAMEL-22820: bridged error handler - Fix NPE if exception on exchange is
null (#20723)
---
.../errorhandler/RedeliveryErrorHandler.java | 6 +-
...idgeErrorHandlerRouteScopedOnExceptionTest.java | 167 +++++++++++++++++++++
2 files changed, 171 insertions(+), 2 deletions(-)
diff --git
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
index 6695e2ab1a8d..29f59e07f72d 100644
---
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
+++
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
@@ -478,7 +478,7 @@ public abstract class RedeliveryErrorHandler extends
ErrorHandlerSupport
// e is never null
Throwable previous =
exchange.getProperty(ExchangePropertyKey.EXCEPTION_CAUGHT, Throwable.class);
- if (previous != null && previous != e) {
+ if (previous != null && previous != e && e != null) {
// a 2nd exception was thrown while handling a previous
exception
// so we need to add the previous as suppressed by the new
exception
// see also FatalFallbackErrorHandler
@@ -501,7 +501,9 @@ public abstract class RedeliveryErrorHandler extends
ErrorHandlerSupport
}
// store the original caused exception in a property, so we can
restore it later
- exchange.setProperty(ExchangePropertyKey.EXCEPTION_CAUGHT, e);
+ if (e != null) {
+ exchange.setProperty(ExchangePropertyKey.EXCEPTION_CAUGHT, e);
+ }
// store where the exception happened
Route rc = ExchangeHelper.getRoute(exchange);
diff --git
a/core/camel-core/src/test/java/org/apache/camel/processor/DefaultConsumerBridgeErrorHandlerRouteScopedOnExceptionTest.java
b/core/camel-core/src/test/java/org/apache/camel/processor/DefaultConsumerBridgeErrorHandlerRouteScopedOnExceptionTest.java
new file mode 100644
index 000000000000..69bc394b25f2
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/processor/DefaultConsumerBridgeErrorHandlerRouteScopedOnExceptionTest.java
@@ -0,0 +1,167 @@
+/*
+ * 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.processor;
+
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.Component;
+import org.apache.camel.Consumer;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.support.DefaultComponent;
+import org.apache.camel.support.DefaultConsumer;
+import org.apache.camel.support.DefaultEndpoint;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+/**
+ *
+ */
+public class DefaultConsumerBridgeErrorHandlerRouteScopedOnExceptionTest
extends ContextTestSupport {
+
+ protected final CountDownLatch latch = new CountDownLatch(1);
+
+ @Test
+ public void testDefaultConsumerBridgeErrorHandler() throws Exception {
+ getMockEndpoint("mock:result").expectedBodiesReceived("Hello World",
"Hello World");
+ getMockEndpoint("mock:a").expectedBodiesReceived("Cannot process");
+ // the other routes does not have onException / error-handler and the
bridge will cause
+ // these routes to fail with a new exception (current behavior in
camel)
+ // so we do not expect the exchange to be routed to mocks
+ getMockEndpoint("mock:b").expectedMessageCount(0);
+ getMockEndpoint("mock:dead").expectedMessageCount(0);
+
+ latch.countDown();
+
+ assertMockEndpointsSatisfied();
+
+ Exception cause =
getMockEndpoint("mock:a").getReceivedExchanges().get(0).getProperty(Exchange.EXCEPTION_CAUGHT,
+ Exception.class);
+ assertNotNull(cause);
+ assertEquals("Simulated", cause.getMessage());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ // START SNIPPET: e1
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ // register our custom component
+ getContext().addComponent("my", new MyComponent());
+
+ // configure the consumer to bridge with the Camel error
+ // handler,
+ // so the above error handler will trigger if exceptions also
+ // occurs inside the consumer
+ from("my:foo?bridgeErrorHandler=true")
+ // configure route scoped on exception
+
.onException(Exception.class).handled(true).to("mock:a").to("direct:error").to("mock:dead").end()
+ .to("log:foo").to("mock:result");
+
+ from("direct:error").to("mock:b").log("Error happened due
${exception.message}");
+ }
+ };
+ // END SNIPPET: e1
+ }
+
+ public class MyComponent extends DefaultComponent {
+
+ @Override
+ protected Endpoint createEndpoint(String uri, String remaining,
Map<String, Object> parameters) {
+ return new MyEndpoint(uri, this);
+ }
+ }
+
+ public class MyEndpoint extends DefaultEndpoint {
+
+ public MyEndpoint(String endpointUri, Component component) {
+ super(endpointUri, component);
+ }
+
+ @Override
+ public Producer createProducer() {
+ return null;
+ }
+
+ @Override
+ public Consumer createConsumer(Processor processor) throws Exception {
+ Consumer answer = new MyConsumer(this, processor);
+ configureConsumer(answer);
+ return answer;
+ }
+
+ @Override
+ public boolean isSingleton() {
+ return true;
+ }
+ }
+
+ public class MyConsumer extends DefaultConsumer {
+
+ private int invoked;
+
+ public MyConsumer(Endpoint endpoint, Processor processor) {
+ super(endpoint, processor);
+ }
+
+ public void doSomething() {
+ try {
+ if (invoked++ == 0) {
+ throw new IllegalArgumentException("Simulated");
+ }
+
+ Exchange exchange = getEndpoint().createExchange();
+ exchange.getIn().setBody("Hello World");
+ getProcessor().process(exchange);
+
+ } catch (Exception e) {
+ getExceptionHandler().handleException("Cannot process", e);
+ }
+ }
+
+ @Override
+ protected void doStart() throws Exception {
+ super.doStart();
+
+ Thread thread = new Thread() {
+ @Override
+ public void run() {
+ try {
+ // do not start before the mocks has been setup and is
+ // ready
+ latch.await(5, TimeUnit.SECONDS);
+ doSomething();
+ doSomething();
+ doSomething();
+ } catch (Exception e) {
+ // ignore
+ }
+ }
+ };
+ thread.start();
+ }
+ }
+}