This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27765-file-rollback-log-exception in repository https://gitbox.apache.org/repos/asf/camel.git
commit 68e00ef9ffc1b296acd4efdcfaa58e0a50d9aa1e Author: Claus Ibsen <[email protected]> AuthorDate: Mon Nov 30 07:38:07 2026 +0100 CAMEL-27765: camel-file - The rollback WARN says which exception caused the rollback The file consumers (file, ftp, ftps, sftp, smb, azure-files) logged the rollback of a file without its cause. When the error handler never logged the exception, such as an exception from a doTry that no doCatch matches or with noErrorHandler, the cause was not in the log at all. The WARN now ends with "due to: <exception>" and carries the stack trace, as the timer consumer does, except for an intended rollback (RollbackExchangeException). The "Rollback file strategy" prefix is kept. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../component/file/GenericFileOnCompletion.java | 15 ++- .../FileConsumerRollbackLogsExceptionTest.java | 132 +++++++++++++++++++++ 2 files changed, 146 insertions(+), 1 deletion(-) diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java index 076603e7e6aa..acac74f8318e 100644 --- a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java +++ b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java @@ -18,9 +18,11 @@ package org.apache.camel.component.file; import org.apache.camel.Exchange; import org.apache.camel.ExchangePropertyKey; +import org.apache.camel.RollbackExchangeException; import org.apache.camel.spi.ExceptionHandler; import org.apache.camel.spi.Synchronization; import org.apache.camel.support.LoggingExceptionHandler; +import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.StringHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -195,7 +197,18 @@ public class GenericFileOnCompletion<T> implements Synchronization { GenericFileProcessStrategy<T> processStrategy, Exchange exchange, GenericFile<T> file) { if (LOG.isWarnEnabled()) { - LOG.warn("Rollback file strategy: {} for file: {}", processStrategy, file); + // say why the file is rolled back: the error handler has not always logged the exception + // (such as an exception from a doTry that no doCatch matches, or with noErrorHandler) + Exception cause = exchange.getException(); + if (cause == null) { + LOG.warn("Rollback file strategy: {} for file: {}", processStrategy, file); + } else if (ObjectHelper.getException(RollbackExchangeException.class, cause) != null) { + // do not log stack trace for intended rollbacks + LOG.warn("Rollback file strategy: {} for file: {} due to: {}", processStrategy, file, cause.toString()); + } else { + LOG.warn("Rollback file strategy: {} for file: {} due to: {}", processStrategy, file, cause.toString(), + cause); + } } if (Boolean.TRUE.equals(endpoint.isIdempotent())) { diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerRollbackLogsExceptionTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerRollbackLogsExceptionTest.java new file mode 100644 index 000000000000..adb9d26f2d94 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerRollbackLogsExceptionTest.java @@ -0,0 +1,132 @@ +/* + * 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.component.file; + +import java.io.IOException; +import java.nio.file.Files; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.log.ConsumingAppender; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.LoggerContext; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Isolated; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * The rollback WARN of the file consumer says which exception caused the rollback, also when the error handler never + * logged it (an exception from a doTry that no doCatch matches). + */ +// isolated: the test captures the log of GenericFileOnCompletion +@Isolated +class FileConsumerRollbackLogsExceptionTest extends ContextTestSupport { + + private static final String LOGGER = GenericFileOnCompletion.class.getName(); + + private final List<LogEvent> warnings = new CopyOnWriteArrayList<>(); + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @BeforeEach + void captureWarnings() { + // an appender added while log4j is still starting is lost + LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + await().atMost(30, TimeUnit.SECONDS).until(ctx::isStarted); + ConsumingAppender.newAppender(LOGGER, getClass().getSimpleName(), Level.WARN, event -> { + if (event.getLevel().isMoreSpecificThan(Level.WARN)) { + warnings.add(event.toImmutable()); + } + }); + } + + @AfterEach + void removeAppender() { + LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + ctx.getConfiguration().removeLogger(LOGGER); + ctx.updateLoggers(); + } + + @Test + void rollbackWarnHasExceptionFromDoTryWithoutMatchingDoCatch() throws Exception { + Files.writeString(testFile("a.txt"), "a"); + + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from(fileUri("?noop=true&initialDelay=0&delay=60000")) + .doTry() + .throwException(new IllegalStateException("boom inside doTry")) + .doCatch(IOException.class) + .log("caught io") + .end(); + } + }); + context.start(); + + LogEvent warning = awaitRollbackWarning(); + String message = warning.getMessage().getFormattedMessage(); + assertTrue(message.startsWith("Rollback file strategy: "), message); + assertTrue(message.endsWith("a.txt] due to: java.lang.IllegalStateException: boom inside doTry"), message); + // with the stack trace + IllegalStateException thrown = assertInstanceOf(IllegalStateException.class, warning.getThrown()); + assertEquals("boom inside doTry", thrown.getMessage()); + } + + @Test + void rollbackWarnHasNoStackTraceForIntendedRollback() throws Exception { + Files.writeString(testFile("a.txt"), "a"); + + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from(fileUri("?noop=true&initialDelay=0&delay=60000")) + .rollback("not now"); + } + }); + context.start(); + + LogEvent warning = awaitRollbackWarning(); + String message = warning.getMessage().getFormattedMessage(); + assertTrue(message.contains("a.txt] due to: org.apache.camel.RollbackExchangeException: not now"), message); + // an intended rollback: no stack trace + assertNull(warning.getThrown()); + } + + private LogEvent awaitRollbackWarning() { + await().atMost(20, TimeUnit.SECONDS).until(() -> warnings.stream() + .anyMatch(e -> e.getMessage().getFormattedMessage().startsWith("Rollback file strategy"))); + return warnings.stream() + .filter(e -> e.getMessage().getFormattedMessage().startsWith("Rollback file strategy")) + .findFirst().orElseThrow(); + } +}
