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 2026/09/10 10:40:48 UTC

(camel) branch camel-4.22.x updated: CAMEL-24651: Fix NPE in ExceptionHelper.stackTraceToString() when exception is null (#26257)

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

davsclaus pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.22.x by this push:
     new 9bd069b800be CAMEL-24651: Fix NPE in ExceptionHelper.stackTraceToString() when exception is null (#26257)
9bd069b800be is described below

commit 9bd069b800beefc03c6bc9b6804d7bf2025871b2
Author: Guillaume Nodet - AI Bot <gn...@gmail.com>
AuthorDate: Thu Sep 10 12:40:41 2026 +0200

    CAMEL-24651: Fix NPE in ExceptionHelper.stackTraceToString() when exception is null (#26257)
    
    The refactoring in f9048eb0a822 that consolidated the duplicated stack trace
    printing code dropped the null check that used to guard the call site in
    LanguageHelper.exceptionStacktrace(). Evaluating the simple expression
    ${exception.stacktrace} on an exchange without an exception therefore passed
    null into ExceptionHelper.stackTraceToString(), which NPE'd on
    e.printStackTrace(printWriter).
    
    Add a null guard at the top of ExceptionHelper.stackTraceToString() so it
    returns null for a null Throwable. Guarding the helper rather than the single
    call site protects every caller and matches the existing behaviour of
    LanguageHelper.exceptionMessage(), which kept its own null check. A regression
    test in SimpleTest covers evaluating ${exception.stacktrace} on an exchange
    with no exception.
    
    Closes #26221
    
    
    (cherry picked from commit 3038c4b331f5be408df5d4b06da7a0d85d16d2bb)
    
    Co-authored-by: Guillaume Nodet <gn...@gmail.com>
    Co-authored-by: Claude <no...@anthropic.com>
---
 .../test/java/org/apache/camel/language/simple/SimpleTest.java    | 8 ++++++++
 .../src/main/java/org/apache/camel/support/ExceptionHelper.java   | 5 ++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index 2bb10c6cc3ac..9e14b4bf6da3 100644
--- a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -641,6 +641,14 @@ public class SimpleTest extends LanguageTestSupport {
         assertTrue(out.contains("at org.apache.camel.language."));
     }
 
+    @Test
+    public void testExceptionStacktraceNoException() {
+        // CAMEL-24651
+        String out = context.resolveLanguage("simple").createExpression("${exception.stacktrace}").evaluate(exchange,
+                String.class);
+        assertNull(out);
+    }
+
     @Test
     public void testException() {
         exchange.setException(new IllegalArgumentException("Just testing"));
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/ExceptionHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/ExceptionHelper.java
index d1e6849546b9..1f37fc3a2b9d 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/ExceptionHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/ExceptionHelper.java
@@ -33,9 +33,12 @@ public class ExceptionHelper {
      * Dumps the stack trace from the given exception to a String
      *
      * @param  e the exception to print the stack trace
-     * @return   A string instance with the stack trace for the given exception
+     * @return   A string instance with the stack trace for the given exception, or null if the exception is null
      */
     public static String stackTraceToString(Throwable e) {
+        if (e == null) {
+            return null;
+        }
         final StringWriter writer = new StringWriter();
         final PrintWriter printWriter = new PrintWriter(writer, true);
         e.printStackTrace(printWriter);