You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2023/03/16 13:26:32 UTC

[camel] branch camel-3.18.x updated: CAMEL-19158: Ensure errors instantiating throwException classes are propagated to the exchange

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

jamesnetherton pushed a commit to branch camel-3.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-3.18.x by this push:
     new a0249cd6781 CAMEL-19158: Ensure errors instantiating throwException classes are propagated to the exchange
a0249cd6781 is described below

commit a0249cd678186d14fc5f6065a0b1cc887a06ceb9
Author: James Netherton <ja...@gmail.com>
AuthorDate: Thu Mar 16 12:14:35 2023 +0000

    CAMEL-19158: Ensure errors instantiating throwException classes are propagated to the exchange
---
 .../camel/processor/ThrowExceptionProcessor.java   |  9 ++--
 .../ThrowExceptionUninstantiatableClassTest.java   | 58 ++++++++++++++++++++++
 2 files changed, 61 insertions(+), 6 deletions(-)

diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/ThrowExceptionProcessor.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/ThrowExceptionProcessor.java
index 727a53dc0d4..fdd7bc92da2 100644
--- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/ThrowExceptionProcessor.java
+++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/ThrowExceptionProcessor.java
@@ -74,12 +74,9 @@ public class ThrowExceptionProcessor extends AsyncProcessorSupport
                 exchange.setException(cause);
             }
         } catch (Throwable e) {
-            if (exception != null) {
-                exchange.setException(
-                        new CamelExchangeException("Error creating new instance of " + exception.getClass(), exchange, e));
-            } else {
-                // TODO
-            }
+            Class<? extends Exception> exceptionClass = exception != null ? exception.getClass() : type;
+            exchange.setException(
+                    new CamelExchangeException("Error creating new instance of " + exceptionClass, exchange, e));
         }
 
         callback.done(true);
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/ThrowExceptionUninstantiatableClassTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/ThrowExceptionUninstantiatableClassTest.java
new file mode 100644
index 00000000000..727499f1c97
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/ThrowExceptionUninstantiatableClassTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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 org.apache.camel.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.fail;
+
+public class ThrowExceptionUninstantiatableClassTest extends ContextTestSupport {
+
+    @Test
+    public void testThrowExceptionWithUninstantiatableClass() throws Exception {
+        getMockEndpoint("mock:start").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        try {
+            template.sendBody("direct:start", "Hello World");
+            fail("Should have thrown an exception");
+        } catch (CamelExecutionException e) {
+            assertIsInstanceOf(NoSuchMethodException.class, e.getCause().getCause());
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start").to("mock:start").throwException(BadException.class, "Forced").to("mock:result");
+            }
+        };
+    }
+
+    static final class BadException extends Exception {
+        private BadException() {
+            // Force an error due to private constructor
+        }
+    }
+}