Hi,

I think I found a potential bug when using a bean to set a property if a 
message is coming through http. I wanted to check here before opening a bug in 
case I am missing something. It is very similar to the following bug which has 
already been fixed: 
https://stackoverflow.com/questions/40293128/camel-rest-service-throws-exception-when-returning-null/40317432#40317432.

The difference is that instead of returning null, an exception is thrown from 
the bean method. When that happen, I see the same behavior as described in the 
stackoverflow post above.  I included an example on how to reproduce at the end 
of this email. Here is what I think happens:

Problem starts in BeanExpression:
1. The exchange is copied, BeanExpression: line 194: Exchange resultExchange = 
ExchangeHelper.createCopy(exchange, true)
2. The out body is then retrieved from the exchange, BeanExpression: line 201: 
result = resultExchange.getOut().getBody();
3. Since out() is null, the in() message is copied to the out(), this makes a 
copy of the HttpMessage, which is copied along with the original ServletRequest 
(which contains a closed stream) DefaultExchange.class line 317.
4. To copy the message from in to out, the method newInstance() from 
HttpMessage class is called. HttpMessage line 85
5. At this point, the new instance of HttpMessage tries to read the stream from 
the original message which is already closed, IOException.

Example just below, I would be expecting the EXCEPTION_CAUGHT to be the 
RuntimeException I throw but instead it's a RuntimeBeanExpressionException 
effectively wrapping a IOException.

public void configureTestException() {
  restDefinitionV1 = restDefinitionV1
    .get("/throwException")
    .route()
     .onException(Exception.class)
       .process(exchange -> {System.out.println("Caught exception of type: " + 
exchange.getProperty(Exchange.EXCEPTION_CAUGHT).getClass());} )
       .handled(true)
     .end()
       .setProperty("test").method(new TestBean(), "throwException")
     .endRest();
}

public static class TestBean {
  public void throwException() {
    throw new RuntimeException("Excepting this exception to be handled by 
onException");
  }
}


Reply via email to