You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/11/18 10:03:03 UTC

[camel] 01/02: CAMEL-17205: camel-core - Error handler with custom prepare processor should have access to failure route id

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

commit 4596aa0a04067c06e0045360bd3698c5f478130a
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Nov 18 10:52:30 2021 +0100

    CAMEL-17205: camel-core - Error handler with custom prepare processor should have access to failure route id
---
 .../errorhandler/RedeliveryErrorHandler.java       | 29 ++++++++++++++--------
 .../DefaultErrorHandlerOnPrepareTest.java          |  6 ++++-
 2 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
index 385fac4..d418a6a 100644
--- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
+++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/errorhandler/RedeliveryErrorHandler.java
@@ -558,7 +558,7 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport
             ee.setException(exchange.getProperty(ExchangePropertyKey.EXCEPTION_CAUGHT, Exception.class));
             // and put failure endpoint back as well
             ee.setProperty(ExchangePropertyKey.FAILURE_ENDPOINT, ee.getProperty(ExchangePropertyKey.TO_ENDPOINT));
-            // and store the route id so we know in which route we failed
+            // and store the route id, so we know in which route we failed
             Route rc = ExchangeHelper.getRoute(ee);
             if (rc != null) {
                 ee.setProperty(ExchangePropertyKey.FAILURE_ROUTE_ID, rc.getRouteId());
@@ -1145,6 +1145,15 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport
                 // reset cached streams so they can be read again
                 MessageHelper.resetStreamCache(exchange.getIn());
 
+                // store the last to endpoint as the failure endpoint
+                exchange.setProperty(ExchangePropertyKey.FAILURE_ENDPOINT,
+                        exchange.getProperty(ExchangePropertyKey.TO_ENDPOINT));
+                // and store the route id, so we know in which route we failed
+                Route rc = ExchangeHelper.getRoute(exchange);
+                if (rc != null) {
+                    exchange.setProperty(ExchangePropertyKey.FAILURE_ROUTE_ID, rc.getRouteId());
+                }
+
                 // invoke custom on prepare
                 if (onPrepareProcessor != null) {
                     try {
@@ -1158,15 +1167,6 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport
 
                 LOG.trace("Failure processor {} is processing Exchange: {}", processor, exchange);
 
-                // store the last to endpoint as the failure endpoint
-                exchange.setProperty(ExchangePropertyKey.FAILURE_ENDPOINT,
-                        exchange.getProperty(ExchangePropertyKey.TO_ENDPOINT));
-                // and store the route id so we know in which route we failed
-                Route rc = ExchangeHelper.getRoute(exchange);
-                if (rc != null) {
-                    exchange.setProperty(ExchangePropertyKey.FAILURE_ROUTE_ID, rc.getRouteId());
-                }
-
                 // fire event as we had a failure processor to handle it, which there is a event for
                 final boolean deadLetterChannel = processor == deadLetter;
 
@@ -1210,6 +1210,15 @@ public abstract class RedeliveryErrorHandler extends ErrorHandlerSupport
                 });
             } else {
                 try {
+                    // store the last to endpoint as the failure endpoint
+                    exchange.setProperty(ExchangePropertyKey.FAILURE_ENDPOINT,
+                            exchange.getProperty(ExchangePropertyKey.TO_ENDPOINT));
+                    // and store the route id, so we know in which route we failed
+                    Route rc = ExchangeHelper.getRoute(exchange);
+                    if (rc != null) {
+                        exchange.setProperty(ExchangePropertyKey.FAILURE_ROUTE_ID, rc.getRouteId());
+                    }
+
                     // invoke custom on prepare
                     if (onPrepareProcessor != null) {
                         try {
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/DefaultErrorHandlerOnPrepareTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/DefaultErrorHandlerOnPrepareTest.java
index 3bff5fe..82ce338 100644
--- a/core/camel-core/src/test/java/org/apache/camel/processor/DefaultErrorHandlerOnPrepareTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/DefaultErrorHandlerOnPrepareTest.java
@@ -38,6 +38,7 @@ public class DefaultErrorHandlerOnPrepareTest extends ContextTestSupport {
         assertTrue(out.isFailed(), "Should be failed");
         assertIsInstanceOf(IllegalArgumentException.class, out.getException());
         assertEquals("Forced", out.getIn().getHeader("FailedBecause"));
+        assertEquals("foo", out.getIn().getHeader("FailedAtRoute"));
     }
 
     @Override
@@ -47,7 +48,9 @@ public class DefaultErrorHandlerOnPrepareTest extends ContextTestSupport {
             public void configure() throws Exception {
                 errorHandler(defaultErrorHandler().onPrepareFailure(new MyPrepareProcessor()));
 
-                from("direct:start").log("Incoming ${body}").throwException(new IllegalArgumentException("Forced"));
+                from("direct:start")
+                        .routeId("foo")
+                        .log("Incoming ${body}").throwException(new IllegalArgumentException("Forced"));
             }
         };
     }
@@ -58,6 +61,7 @@ public class DefaultErrorHandlerOnPrepareTest extends ContextTestSupport {
         public void process(Exchange exchange) throws Exception {
             Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
             exchange.getIn().setHeader("FailedBecause", cause.getMessage());
+            exchange.getIn().setHeader("FailedAtRoute", exchange.getProperty(Exchange.FAILURE_ROUTE_ID, String.class));
         }
     }
 }