[ 
https://issues.apache.org/jira/browse/CAMEL-24651?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18113149#comment-18113149
 ] 

Guillaume Nodet commented on CAMEL-24651:
-----------------------------------------

This issue is being investigated by a coding agent (on behalf of gnodet).

Initial triage confirms this is a regression introduced by a refactoring commit 
that consolidated duplicated code for printing stack traces. The null check for 
the exception was removed during that refactoring. When ${exception.stacktrace} 
is evaluated on an exchange with no exception, the code now throws NPE instead 
of returning null/empty. A fix has been dispatched.

_Note: This comment was generated by an AI coding agent and requires manual 
verification._

> Regression on Camel 4.x NPE on evaluation of a Simple expression 
> ${exception.stacktrace}
> ----------------------------------------------------------------------------------------
>
>                 Key: CAMEL-24651
>                 URL: https://issues.apache.org/jira/browse/CAMEL-24651
>             Project: Camel
>          Issue Type: Bug
>    Affects Versions: 4.18.4, 4.22.0
>            Reporter: Xilai Dai
>            Assignee: Guillaume Nodet
>            Priority: Minor
>
> {code:java}
> from("timer:Timer_1?repeatCount=1&delay=1000")
> .process(new org.apache.camel.Processor() {                           
>         public void process(org.apache.camel.Exchange exchange) throws 
> Exception {
>                       // throw new IllegalArgumentException("Simulated 
> processing error");
>       }
> })            
> .setBody().simple("${exception.stacktrace}"); {code}
> {code:java}
> java.lang.NullPointerException: Cannot invoke 
> "java.lang.Throwable.printStackTrace(java.io.PrintWriter)" because "e" is 
> null  
> at 
> org.apache.camel.support.ExceptionHelper.stackTraceToString(ExceptionHelper.java:41)
>        
> at 
> org.apache.camel.support.LanguageHelper.exceptionStacktrace(LanguageHelper.java:80)
>         
> at 
> org.apache.camel.support.builder.ExpressionBuilder$16.evaluate(ExpressionBuilder.java:641)
>  
> at 
> org.apache.camel.support.ExpressionAdapter.evaluate(ExpressionAdapter.java:45)
>      
> at 
> org.apache.camel.processor.SetBodyProcessor.process(SetBodyProcessor.java:45) 
>      
> at 
> org.apache.camel.support.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:104)
>  {code}
> The issue is not present on Camel 3.x and the null value of 
> exception.stacktrace is properly handled.
> Proposed fixes:
> {code:java}
> // org.apache.camel.support.LanguageHelper
> public static String exceptionStacktrace(Exchange exchange) {
>     Exception exception = exception(exchange);
>     if (exception == null) {
>         return null;
>     }
>     return ExceptionHelper.stackTraceToString(exception);
> } {code}
> {code:java}
> // org.apache.camel.support.ExceptionHelper
> 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);
>     return writer.toString();
> } {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to