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


The following commit(s) were added to refs/heads/main by this push:
     new d2ead12af2b1 CAMEL-27483: camel-xml-jaxp - XmlConverter no longer logs 
a parse error at ERROR before the parser throws it (#30689)
d2ead12af2b1 is described below

commit d2ead12af2b1d6f12fbce8ef226fb99c40980f66
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Nov 23 22:11:16 2026 +0100

    CAMEL-27483: camel-xml-jaxp - XmlConverter no longer logs a parse error at 
ERROR before the parser throws it (#30689)
    
    The error handler of the DocumentBuilder logged fatal and recoverable parse 
errors at ERROR with the stack trace, and
    the parser then threw the same SAXParseException. A route that handles the 
exception still got an ERROR line for each
    body that is not well-formed XML. A fatal error is now thrown by the 
handler, and warnings and recoverable errors are
    logged at DEBUG.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../jaxp/XmlConverterParseErrorLogTest.java        | 93 ++++++++++++++++++++++
 .../apache/camel/converter/jaxp/XmlConverter.java  | 11 ++-
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |  7 ++
 3 files changed, 108 insertions(+), 3 deletions(-)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterParseErrorLogTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterParseErrorLogTest.java
new file mode 100644
index 000000000000..8d3b279e04d4
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/converter/jaxp/XmlConverterParseErrorLogTest.java
@@ -0,0 +1,93 @@
+/*
+ * 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.converter.jaxp;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.xml.sax.SAXParseException;
+
+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.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Property;
+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.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+/**
+ * A body that is not well-formed XML fails the conversion with the parse 
exception, and is not also logged at ERROR, so
+ * a route that handles the exception gets no ERROR line for each such message.
+ */
+@Isolated
+class XmlConverterParseErrorLogTest {
+
+    private final List<String> errors = new CopyOnWriteArrayList<>();
+    private AbstractAppender appender;
+
+    @BeforeEach
+    void captureErrors() {
+        appender = new AbstractAppender("xml-converter-errors-" + 
UUID.randomUUID(), null, null, true, Property.EMPTY_ARRAY) {
+            @Override
+            public void append(LogEvent event) {
+                if 
(event.getLoggerName().startsWith("org.apache.camel.converter.jaxp")) {
+                    errors.add(event.getLevel() + " " + 
event.getMessage().getFormattedMessage());
+                }
+            }
+        };
+        appender.start();
+        LoggerContext logging = (LoggerContext) LogManager.getContext(false);
+        logging.getConfiguration().getRootLogger().addAppender(appender, 
Level.WARN, null);
+        logging.updateLoggers();
+    }
+
+    @AfterEach
+    void stopCapture() {
+        LoggerContext logging = (LoggerContext) LogManager.getContext(false);
+        
logging.getConfiguration().getRootLogger().removeAppender(appender.getName());
+        logging.updateLoggers();
+        appender.stop();
+    }
+
+    @Test
+    void textThatIsNotXmlThrowsWithoutLogging() {
+        XmlConverter converter = new XmlConverter();
+
+        SAXParseException e = assertThrows(SAXParseException.class, () -> 
converter.toDOMDocument("How long do I have?", null));
+
+        assertEquals("Content is not allowed in prolog.", e.getMessage());
+        assertEquals(List.of(), errors);
+    }
+
+    @Test
+    void xmlThatIsNotWellFormedThrowsWithoutLogging() {
+        XmlConverter converter = new XmlConverter();
+        byte[] body = "<order><item></order>".getBytes(StandardCharsets.UTF_8);
+
+        assertThrows(SAXParseException.class, () -> 
converter.toDOMDocument(body, null));
+
+        assertEquals(List.of(), errors);
+    }
+}
diff --git 
a/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
 
b/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
index be8fe607a57d..1fd97b6b2730 100644
--- 
a/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
+++ 
b/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java
@@ -1236,21 +1236,26 @@ public class XmlConverter {
         return sfactory;
     }
 
+    /**
+     * Logs warnings and recoverable errors of the parser at DEBUG, and throws 
a fatal error: a body that is not
+     * well-formed XML fails the conversion with the exception, which the 
route can handle (doTry, onException), so it
+     * is not logged here as well. Without an error handler, the JDK prints 
the errors to the standard error.
+     */
     private static class DocumentBuilderLoggingErrorHandler implements 
ErrorHandler {
 
         @Override
         public void warning(SAXParseException exception) throws SAXException {
-            LOG.warn(exception.getMessage(), exception);
+            LOG.debug("XML parser warning: {}", exception.getMessage(), 
exception);
         }
 
         @Override
         public void error(SAXParseException exception) throws SAXException {
-            LOG.error(exception.getMessage(), exception);
+            LOG.debug("XML parser error: {}", exception.getMessage(), 
exception);
         }
 
         @Override
         public void fatalError(SAXParseException exception) throws 
SAXException {
-            LOG.error(exception.getMessage(), exception);
+            throw exception;
         }
     }
 
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 07425a274750..cd2a6368888f 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -4593,6 +4593,13 @@ provider is detected as before.
   nothing), and a route that cannot be read, such as invalid YAML, exits with 
`1` and the error of the parser (it failed
   with an internal error, `70`, and a stack trace).
 
+=== camel-xml-jaxp - XML parse errors are no longer logged at ERROR by the 
type converter
+
+When a message body that is not well-formed XML is converted to a DOM 
`Document` (for example by `xpath`), the
+`XmlConverter` used to log the parse error at ERROR, with its stack trace, and 
then fail with the same
+`SAXParseException`. It now only fails with the exception, which the route can 
handle (`doTry`, `onException`) or
+which the error handler logs. Parser warnings and recoverable errors are 
logged at DEBUG.
+
 === camel-xml-io, camel-yaml-io, camel-java-io - route dumps keep options set 
to their default value
 
 The XML, YAML and Java route dumps (`camel transform route`, `camel validate 
normalize`, `camel.main.dumpRoutes`, the

Reply via email to