This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new b79d9209323 CAMEL-18252: Add exception to exchange as property 
CamelExceptionCaught (#7967)
b79d9209323 is described below

commit b79d92093239d6fa176c5bfd4329a748a54ea155
Author: Benjamin Graf <[email protected]>
AuthorDate: Fri Jul 1 17:06:12 2022 +0200

    CAMEL-18252: Add exception to exchange as property CamelExceptionCaught 
(#7967)
---
 .../processor/OnCompletionBridgeErrorHandler.java  | 114 +++++++++++++++++++++
 .../BridgeExceptionHandlerToErrorHandler.java      |   1 +
 2 files changed, 115 insertions(+)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/processor/OnCompletionBridgeErrorHandler.java
 
b/core/camel-core/src/test/java/org/apache/camel/processor/OnCompletionBridgeErrorHandler.java
new file mode 100644
index 00000000000..b51df246516
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/processor/OnCompletionBridgeErrorHandler.java
@@ -0,0 +1,114 @@
+/*
+ * 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 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.DefaultEndpoint;
+import org.apache.camel.support.ScheduledPollConsumer;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class OnCompletionBridgeErrorHandler extends ContextTestSupport {
+
+    protected final CountDownLatch latch = new CountDownLatch(1);
+
+    @Test
+    public void test() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+        getMockEndpoint("mock:dead").expectedMessageCount(1);
+        latch.countDown();
+        assertMockEndpointsSatisfied();
+        Exception cause = 
getMockEndpoint("mock:dead").getReceivedExchanges().get(0).getProperty(Exchange.EXCEPTION_CAUGHT,
 Exception.class);
+        assertNotNull(cause);
+        assertEquals("Simulated", cause.getMessage());
+
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                getContext().addComponent("my", new MyComponent());
+                errorHandler(noErrorHandler());
+
+                from("my:foo?bridgeErrorHandler=true")
+                        .onCompletion()
+                        .to("mock:dead")
+                        .end()
+                        .to("mock:result");
+            }
+        };
+    }
+
+    public static class MyComponent extends DefaultComponent {
+
+        @Override
+        protected Endpoint createEndpoint(String uri, String remaining, 
Map<String, Object> parameters) throws Exception {
+            return new MyEndpoint(uri, this);
+        }
+    }
+
+    public static class MyEndpoint extends DefaultEndpoint {
+
+        public MyEndpoint(String endpointUri, Component component) {
+            super(endpointUri, component);
+        }
+
+        @Override
+        public Producer createProducer() throws Exception {
+            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 static class MyConsumer extends ScheduledPollConsumer {
+
+        public MyConsumer(Endpoint endpoint, Processor processor) {
+            super(endpoint, processor);
+        }
+
+        @Override
+        protected int poll() throws Exception {
+            throw new IllegalArgumentException("Simulated");
+        }
+    }
+}
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/BridgeExceptionHandlerToErrorHandler.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/BridgeExceptionHandlerToErrorHandler.java
index 8e2094ef474..88d6e806453 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/BridgeExceptionHandlerToErrorHandler.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/BridgeExceptionHandlerToErrorHandler.java
@@ -65,6 +65,7 @@ public class BridgeExceptionHandlerToErrorHandler implements 
ExceptionHandler {
 
         // set the caused exception
         exchange.setException(exception);
+        exchange.setProperty(ExchangePropertyKey.EXCEPTION_CAUGHT, exception);
         // and the message
         exchange.getIn().setBody(message);
         // mark as bridged

Reply via email to