Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-15200

Processor setBody ends up in bodyType: null

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Not A Bug
    • Affects Version/s: 3.3.0
    • Fix Version/s: 3.x
    • Component/s: came-core
    • Labels:
      None
    • Environment:

      Java 11

      Camel 3.3.0

    • Estimated Complexity:
      Unknown

      Description

      Issue

      NOTE: I will try to come back with a fixing PR ASAP.
       
      The processor

      package com.company.processors;
      
      import com.company.api.model.Error;
      import org.apache.camel.Exchange;
      import org.apache.camel.Processor;
      
      public class ToError implements Processor {
          @Override
          public void process(Exchange exchange) {
              Throwable responseException = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
      
              Error response = new Error() {{
                  setMessage(responseException.getMessage());
                  setDetails(responseException.getCause().getMessage());
              }};
      
              exchange.getIn().setBody(response);
          }
      }
      

      with the route

      .doCatch(Exception.class)
      	.process(new ToError())
      	.to("log:error")
      

      ends up in

      2020-06-17 10:39:03.299  INFO 2928 --- [ult-workqueue-2] error                                    : Exchange[ExchangePattern: InOut, BodyType: null, Body: class Error {
          message: Connection reset
          details: null
      }]

      where BodyType: null causes consequent problems e.g. the route

      .end()
      .transform().body().marshal().json(JsonLibrary.Gson).endRest();
      

      returns a ResponseEntity with no body.

      Reason

      It took one day to find the villain The reason was that Java instance initialization via setters inside `{{}}`s

      Workaround

      Replacing

      Error response = new Error() {{
          setMessage(responseException.getMessage());
          setDetails(responseException.getCause().getMessage());
      }};
      

      with

      Error response = new Error();
      response.setMessage(responseException.getMessage());
      response.setDetails(responseException.getCause().getMessage());
      

      fixes issue.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              yasserzamani Yasser Zamani
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: