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

Croway 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 11b322fef6af CAMEL-24594: Log REST DSL response marshalling failures
11b322fef6af is described below

commit 11b322fef6afd5c01f76cd240962d40b9fef190a
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Sep 3 09:23:27 2026 +0200

    CAMEL-24594: Log REST DSL response marshalling failures
    
    When the REST DSL response binding (bindingMode json/xml) fails to
    marshal the response body, the failure happens in RestBindingAdvice#after
    (post-routing) so it is not routed through the error handler and was
    previously invisible - nothing was logged at any level. This made a
    downstream serialization failure very hard to diagnose.
    
    Log the exception (WARN, with stacktrace) when marshalling the response
    body fails, bringing the post-routing binding step in line with how every
    other failure in Camel surfaces. The response code behaviour is
    unchanged: an explicitly set HTTP_RESPONSE_CODE still wins, consistent
    with plain routes.
    
    Extended the regression test with a variant whose processor sets
    HTTP_RESPONSE_CODE=200 before the failing marshal, to pin that an
    explicitly set code still wins.
    
    Co-authored-by: Claude Opus 4.8 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../vertx/VertxRestBindingMarshalFailureTest.java  | 38 ++++++++++++++++++++++
 .../camel/support/processor/RestBindingAdvice.java | 10 ++++++
 2 files changed, 48 insertions(+)

diff --git 
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxRestBindingMarshalFailureTest.java
 
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxRestBindingMarshalFailureTest.java
index 059e9b05697f..c95a17f5d282 100644
--- 
a/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxRestBindingMarshalFailureTest.java
+++ 
b/components/camel-platform-http-vertx/src/test/java/org/apache/camel/component/platform/http/vertx/VertxRestBindingMarshalFailureTest.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.platform.http.vertx;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.model.rest.RestBindingMode;
 import org.junit.jupiter.api.Test;
@@ -108,6 +109,43 @@ public class VertxRestBindingMarshalFailureTest {
         }
     }
 
+    @Test
+    public void testExplicitResponseCodeStillWinsOnMarshalFailure() throws 
Exception {
+        final CamelContext context = 
VertxPlatformHttpEngineTest.createCamelContext();
+
+        try {
+            context.addRoutes(new RouteBuilder() {
+                @Override
+                public void configure() {
+                    restConfiguration().bindingMode(RestBindingMode.json);
+
+                    rest("/demo")
+                            .get("/{id}").to("direct:demo");
+
+                    from("direct:demo")
+                            // an explicitly set response code must still win, 
even though the response marshalling
+                            // fails afterwards (the failure is logged 
server-side, but does not override the code)
+                            .process(e -> {
+                                
e.getMessage().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
+                                e.getMessage().setBody(new BadPojo());
+                            });
+                }
+            });
+
+            VertxPlatformHttpEngineTest.startCamelContext(context);
+
+            given()
+                    .when()
+                    .get("/demo/42")
+                    .then()
+                    // the explicitly set 200 wins over the marshal failure, 
consistent with plain routes
+                    .statusCode(200)
+                    .body(emptyString());
+        } finally {
+            context.stop();
+        }
+    }
+
     /**
      * A POJO that always fails to marshal because its getter throws.
      */
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
index 2dbc10e6e159..204ace3990ea 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/RestBindingAdvice.java
@@ -38,6 +38,8 @@ import org.apache.camel.support.MessageHelper;
 import org.apache.camel.support.service.ServiceHelper;
 import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Used for Rest DSL with binding to json/xml for incoming requests and 
outgoing responses.
@@ -51,6 +53,8 @@ import org.apache.camel.util.ObjectHelper;
  */
 public class RestBindingAdvice extends ServiceSupport implements 
CamelInternalProcessorAdvice<Map<String, Object>> {
 
+    private static final Logger LOG = 
LoggerFactory.getLogger(RestBindingAdvice.class);
+
     private static final String STATE_KEY_DO_MARSHAL = "doMarshal";
     private static final String STATE_KEY_ACCEPT = "accept";
     private static final String STATE_JSON = "json";
@@ -465,6 +469,12 @@ public class RestBindingAdvice extends ServiceSupport 
implements CamelInternalPr
                 }
             }
         } catch (Exception e) {
+            // the response marshalling happens after routing has completed, 
so this failure is not routed through
+            // the error handler and would otherwise be invisible. Log it so 
operators can diagnose why the response
+            // could not be bound (see CAMEL-24594).
+            LOG.warn("Error marshalling REST DSL response body for exchange: 
{} due to: {}."
+                     + " This exception is set on the exchange to fail the 
response.",
+                    exchange, e.getMessage(), e);
             exchange.setException(e);
         }
 

Reply via email to