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

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


The following commit(s) were added to refs/heads/camel-4.4.x by this push:
     new bc3caab52cc CAMEL-20873: Handle potential failures in 
VertxPlatformHttpSupport.writeResponse
bc3caab52cc is described below

commit bc3caab52cc6b8c1f05d7df6ad296e25a238e14d
Author: James Netherton <[email protected]>
AuthorDate: Thu Jun 13 13:25:37 2024 +0100

    CAMEL-20873: Handle potential failures in 
VertxPlatformHttpSupport.writeResponse
---
 .../http/vertx/VertxPlatformHttpSupport.java       | 36 ++++++++--------
 .../http/vertx/VertxPlatformHttpEngineTest.java    | 49 ++++++++++++++++++++++
 2 files changed, 67 insertions(+), 18 deletions(-)

diff --git 
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
 
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
index 9230343a36e..29b493037ee 100644
--- 
a/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
+++ 
b/components/camel-platform-http-vertx/src/main/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpSupport.java
@@ -170,27 +170,27 @@ public final class VertxPlatformHttpSupport {
 
     static Future<Void> writeResponse(
             RoutingContext ctx, Exchange camelExchange, HeaderFilterStrategy 
headerFilterStrategy, boolean muteExceptions) {
-        final Object body = toHttpResponse(ctx.response(), 
camelExchange.getMessage(), headerFilterStrategy, muteExceptions);
         final Promise<Void> promise = Promise.promise();
-
-        if (body == null) {
-            LOGGER.trace("No payload to send as reply for exchange: {}", 
camelExchange);
-            ctx.end();
-            promise.complete();
-        } else if (body instanceof String) {
-            ctx.end((String) body);
-            promise.complete();
-        } else if (body instanceof InputStream) {
-            writeResponseAs(promise, ctx, (InputStream) body);
-        } else if (body instanceof Buffer) {
-            ctx.end((Buffer) body);
-            promise.complete();
-        } else {
-            try {
+        try {
+            final Object body
+                    = toHttpResponse(ctx.response(), 
camelExchange.getMessage(), headerFilterStrategy, muteExceptions);
+            if (body == null) {
+                LOGGER.trace("No payload to send as reply for exchange: {}", 
camelExchange);
+                ctx.end();
+                promise.complete();
+            } else if (body instanceof String) {
+                ctx.end((String) body);
+                promise.complete();
+            } else if (body instanceof InputStream) {
+                writeResponseAs(promise, ctx, (InputStream) body);
+            } else if (body instanceof Buffer) {
+                ctx.end((Buffer) body);
+                promise.complete();
+            } else {
                 writeResponseAsFallback(promise, camelExchange, body, ctx);
-            } catch (NoTypeConversionAvailableException e) {
-                promise.fail(e);
             }
+        } catch (Exception e) {
+            promise.fail(e);
         }
 
         return promise.future();
diff --git 
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
 
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
index 37819e346ee..3a04e5b2f7d 100644
--- 
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
+++ 
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxPlatformHttpEngineTest.java
@@ -20,6 +20,7 @@ import java.io.File;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -1019,6 +1020,54 @@ public class VertxPlatformHttpEngineTest {
         }
     }
 
+    @Test
+    public void testResponseTypeConversionErrorHandled() throws Exception {
+        final CamelContext context = createCamelContext();
+
+        try {
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() {
+                    from("platform-http:/error/response")
+                            // Set the response to something that can't be 
type converted
+                            .setBody().constant(Collections.EMPTY_SET);
+                }
+            });
+
+            context.start();
+
+            get("/error/response")
+                    .then()
+                    .statusCode(500);
+        } finally {
+            context.stop();
+        }
+    }
+
+    @Test
+    public void testResponseBadQueryParamErrorHandled() throws Exception {
+        final CamelContext context = createCamelContext();
+
+        try {
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() {
+                    from("platform-http:/error/response")
+                            .setBody().constant("Error");
+                }
+            });
+
+            context.start();
+
+            // Add a query param that Vert.x cannot handle
+            get("/error/response?::")
+                    .then()
+                    .statusCode(500);
+        } finally {
+            context.stop();
+        }
+    }
+
     static CamelContext createCamelContext() throws Exception {
         return createCamelContext(null);
     }

Reply via email to