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

jamesnetherton 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 c223a857a43 CAMEL-20873: Handle potential failures in 
VertxPlatformHttpSupport.writeResponse
c223a857a43 is described below

commit c223a857a433b65673d90157d594b0ec5040b16a
Author: James Netherton <[email protected]>
AuthorDate: Thu Jun 13 13:04:35 2024 +0100

    CAMEL-20873: Handle potential failures in 
VertxPlatformHttpSupport.writeResponse
---
 .../http/vertx/VertxPlatformHttpSupport.java       | 35 ++++++++--------
 .../http/vertx/VertxPlatformHttpEngineTest.java    | 49 ++++++++++++++++++++++
 2 files changed, 66 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 887064f6ddf..96c8d893d0b 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
@@ -178,27 +178,26 @@ public final class VertxPlatformHttpSupport {
 
     static Future<Void> writeResponse(
             RoutingContext ctx, Exchange camelExchange, HeaderFilterStrategy 
headerFilterStrategy, boolean muteExceptions) {
-        final Object body = toHttpResponse(ctx, 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, 
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 630a24855f6..18b8d9070a9 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;
@@ -1028,6 +1029,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