This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-3.20.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.20.x by this push: new f4f291a5362 exception in onCompletionProcessor should be suppressed, other than be lost (#10944) f4f291a5362 is described below commit f4f291a53621b3ae919c50a57f65badf9b80e6c9 Author: EvanMi <43713766+eva...@users.noreply.github.com> AuthorDate: Fri Aug 4 01:45:14 2023 +0800 exception in onCompletionProcessor should be suppressed, other than be lost (#10944) CAMEL-19688: exception in onCompletionProcessor should be suppressed, other than be lost Co-authored-by: mipengcheng <mipengche...@jd.com> --- .../camel/processor/OnCompletionProcessor.java | 4 ++ ...nModeBeforeConsumerExceptionSuppressedTest.java | 62 ++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java index 58c708c29cc..05955a73e7f 100644 --- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java +++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/OnCompletionProcessor.java @@ -202,6 +202,10 @@ public class OnCompletionProcessor extends AsyncProcessorSupport implements Trac ee.setRollbackOnlyLast(rollbackOnlyLast); ee.setRedeliveryExhausted(exhausted); if (cause != null) { + // if there is any exception in onCompletionProcessor, the exception should be suppressed + if (exchange.isFailed()) { + cause.addSuppressed(exchange.getException()); + } ee.setException(cause); } } diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/OnCompletionModeBeforeConsumerExceptionSuppressedTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/OnCompletionModeBeforeConsumerExceptionSuppressedTest.java new file mode 100644 index 00000000000..69f821f0f7c --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/processor/OnCompletionModeBeforeConsumerExceptionSuppressedTest.java @@ -0,0 +1,62 @@ +/* + * 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.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class OnCompletionModeBeforeConsumerExceptionSuppressedTest extends ContextTestSupport { + + @Test + public void testOnCompletionModeBeforeConsumerExceptionSuppressed() { + Exchange out = template.request("direct:foo", exchange -> exchange.getIn().setBody("Camel")); + Exception fooException = out.getException(); + assertIsInstanceOf(FooException.class, fooException); + Throwable[] suppressed = fooException.getSuppressed(); + assertEquals(suppressed.length, 1); + assertIsInstanceOf(SuppressedException.class, suppressed[0]); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:foo") + .process(exchange -> { + throw new FooException(); + }) + .onCompletion() + .modeBeforeConsumer() + .process(exchange -> { + throw new SuppressedException(); + }) + .end(); + } + }; + } + + public static class FooException extends RuntimeException { + } + + public static class SuppressedException extends RuntimeException { + } +}