Apache Camel: How to get the exception message inside the doCatch() block?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I need to return the message from a thrown exception, or put it in the outmessage. But it does not print the correct message on the frontend.



The camel docs suggest using .transform(simple?...) .handled(true) but most of it is deprecated.



What's the correct way of doing this?



Response:

<418 I'm a teapot,simple{${exception.message}},{}>




Route



from("direct:csv")
.doTry()
.process(doSomeThingWithTheFileProcessor)
.doCatch(Exception.class)
.process(e -> {
e.getOut().setBody(new ResponseEntity<String>(exceptionMessage().toString(), HttpStatus.I_AM_A_TEAPOT));
}).stop()
.end()
.process(finalizeTheRouteProcessor);



doSomethingWithFileProcessor



public void process(Exchange exchange) throws Exception {
String filename = exchange.getIn().getHeader("CamelFileName", String.class);

MyFile mf = repo.getFile(filename); //throws exception

exchange.getOut().setBody(exchange.getIn().getBody());
exchange.getOut().setHeader("CamelFileName", exchange.getIn().getHeader("CamelFileName"));
}









share|improve this question





























    2















    I need to return the message from a thrown exception, or put it in the outmessage. But it does not print the correct message on the frontend.



    The camel docs suggest using .transform(simple?...) .handled(true) but most of it is deprecated.



    What's the correct way of doing this?



    Response:

    <418 I'm a teapot,simple{${exception.message}},{}>




    Route



    from("direct:csv")
    .doTry()
    .process(doSomeThingWithTheFileProcessor)
    .doCatch(Exception.class)
    .process(e -> {
    e.getOut().setBody(new ResponseEntity<String>(exceptionMessage().toString(), HttpStatus.I_AM_A_TEAPOT));
    }).stop()
    .end()
    .process(finalizeTheRouteProcessor);



    doSomethingWithFileProcessor



    public void process(Exchange exchange) throws Exception {
    String filename = exchange.getIn().getHeader("CamelFileName", String.class);

    MyFile mf = repo.getFile(filename); //throws exception

    exchange.getOut().setBody(exchange.getIn().getBody());
    exchange.getOut().setHeader("CamelFileName", exchange.getIn().getHeader("CamelFileName"));
    }









    share|improve this question

























      2












      2








      2








      I need to return the message from a thrown exception, or put it in the outmessage. But it does not print the correct message on the frontend.



      The camel docs suggest using .transform(simple?...) .handled(true) but most of it is deprecated.



      What's the correct way of doing this?



      Response:

      <418 I'm a teapot,simple{${exception.message}},{}>




      Route



      from("direct:csv")
      .doTry()
      .process(doSomeThingWithTheFileProcessor)
      .doCatch(Exception.class)
      .process(e -> {
      e.getOut().setBody(new ResponseEntity<String>(exceptionMessage().toString(), HttpStatus.I_AM_A_TEAPOT));
      }).stop()
      .end()
      .process(finalizeTheRouteProcessor);



      doSomethingWithFileProcessor



      public void process(Exchange exchange) throws Exception {
      String filename = exchange.getIn().getHeader("CamelFileName", String.class);

      MyFile mf = repo.getFile(filename); //throws exception

      exchange.getOut().setBody(exchange.getIn().getBody());
      exchange.getOut().setHeader("CamelFileName", exchange.getIn().getHeader("CamelFileName"));
      }









      share|improve this question














      I need to return the message from a thrown exception, or put it in the outmessage. But it does not print the correct message on the frontend.



      The camel docs suggest using .transform(simple?...) .handled(true) but most of it is deprecated.



      What's the correct way of doing this?



      Response:

      <418 I'm a teapot,simple{${exception.message}},{}>




      Route



      from("direct:csv")
      .doTry()
      .process(doSomeThingWithTheFileProcessor)
      .doCatch(Exception.class)
      .process(e -> {
      e.getOut().setBody(new ResponseEntity<String>(exceptionMessage().toString(), HttpStatus.I_AM_A_TEAPOT));
      }).stop()
      .end()
      .process(finalizeTheRouteProcessor);



      doSomethingWithFileProcessor



      public void process(Exchange exchange) throws Exception {
      String filename = exchange.getIn().getHeader("CamelFileName", String.class);

      MyFile mf = repo.getFile(filename); //throws exception

      exchange.getOut().setBody(exchange.getIn().getBody());
      exchange.getOut().setHeader("CamelFileName", exchange.getIn().getHeader("CamelFileName"));
      }






      apache spring-boot apache-camel dsl






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 15:01









      EdwardEdward

      5252929




      5252929
























          2 Answers
          2






          active

          oldest

          votes


















          4














          There is a lot of ways how to do it. All of it is correct, choose your favourite depending on complexity of error handling. I have published examples in this gist. None of it is deprecated in Camel version 2.22.0.



          With Processor



          from("direct:withProcessor")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .process(new Processor() {
          @Override
          public void process(Exchange exchange) throws Exception {
          final Throwable ex = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
          exchange.getIn().setBody(ex.getMessage());
          }
          })
          .end();


          With Simple language



          from("direct:withSimple")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .transform().simple("${exception.message}")
          .end();


          With setBody



          from("direct:withValueBuilder")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .setBody(exceptionMessage())
          .end();





          share|improve this answer
























          • This works, thanks!

            – Edward
            Nov 27 '18 at 6:57



















          2














          In the doCatch() Camel moves the exception into a property on the exchange with the key Exchange.EXCEPTION_CAUGHT (http://camel.apache.org/why-is-the-exception-null-when-i-use-onexception.html).



          So you can use



          e.getOut().setBody(new ResponseEntity<String>(e.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class).getMessage(), HttpStatus.OK));





          share|improve this answer
























          • +1 for the answer, but I chose @Bedla's answer because it's more complete and provides multiple solutions to the problem.

            – Edward
            Nov 27 '18 at 6:57












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53483879%2fapache-camel-how-to-get-the-exception-message-inside-the-docatch-block%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          There is a lot of ways how to do it. All of it is correct, choose your favourite depending on complexity of error handling. I have published examples in this gist. None of it is deprecated in Camel version 2.22.0.



          With Processor



          from("direct:withProcessor")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .process(new Processor() {
          @Override
          public void process(Exchange exchange) throws Exception {
          final Throwable ex = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
          exchange.getIn().setBody(ex.getMessage());
          }
          })
          .end();


          With Simple language



          from("direct:withSimple")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .transform().simple("${exception.message}")
          .end();


          With setBody



          from("direct:withValueBuilder")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .setBody(exceptionMessage())
          .end();





          share|improve this answer
























          • This works, thanks!

            – Edward
            Nov 27 '18 at 6:57
















          4














          There is a lot of ways how to do it. All of it is correct, choose your favourite depending on complexity of error handling. I have published examples in this gist. None of it is deprecated in Camel version 2.22.0.



          With Processor



          from("direct:withProcessor")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .process(new Processor() {
          @Override
          public void process(Exchange exchange) throws Exception {
          final Throwable ex = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
          exchange.getIn().setBody(ex.getMessage());
          }
          })
          .end();


          With Simple language



          from("direct:withSimple")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .transform().simple("${exception.message}")
          .end();


          With setBody



          from("direct:withValueBuilder")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .setBody(exceptionMessage())
          .end();





          share|improve this answer
























          • This works, thanks!

            – Edward
            Nov 27 '18 at 6:57














          4












          4








          4







          There is a lot of ways how to do it. All of it is correct, choose your favourite depending on complexity of error handling. I have published examples in this gist. None of it is deprecated in Camel version 2.22.0.



          With Processor



          from("direct:withProcessor")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .process(new Processor() {
          @Override
          public void process(Exchange exchange) throws Exception {
          final Throwable ex = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
          exchange.getIn().setBody(ex.getMessage());
          }
          })
          .end();


          With Simple language



          from("direct:withSimple")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .transform().simple("${exception.message}")
          .end();


          With setBody



          from("direct:withValueBuilder")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .setBody(exceptionMessage())
          .end();





          share|improve this answer













          There is a lot of ways how to do it. All of it is correct, choose your favourite depending on complexity of error handling. I have published examples in this gist. None of it is deprecated in Camel version 2.22.0.



          With Processor



          from("direct:withProcessor")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .process(new Processor() {
          @Override
          public void process(Exchange exchange) throws Exception {
          final Throwable ex = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
          exchange.getIn().setBody(ex.getMessage());
          }
          })
          .end();


          With Simple language



          from("direct:withSimple")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .transform().simple("${exception.message}")
          .end();


          With setBody



          from("direct:withValueBuilder")
          .doTry()
          .process(new ThrowExceptionProcessor())
          .doCatch(Exception.class)
          .setBody(exceptionMessage())
          .end();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 19:08









          BedlaBedla

          1,8612618




          1,8612618













          • This works, thanks!

            – Edward
            Nov 27 '18 at 6:57



















          • This works, thanks!

            – Edward
            Nov 27 '18 at 6:57

















          This works, thanks!

          – Edward
          Nov 27 '18 at 6:57





          This works, thanks!

          – Edward
          Nov 27 '18 at 6:57













          2














          In the doCatch() Camel moves the exception into a property on the exchange with the key Exchange.EXCEPTION_CAUGHT (http://camel.apache.org/why-is-the-exception-null-when-i-use-onexception.html).



          So you can use



          e.getOut().setBody(new ResponseEntity<String>(e.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class).getMessage(), HttpStatus.OK));





          share|improve this answer
























          • +1 for the answer, but I chose @Bedla's answer because it's more complete and provides multiple solutions to the problem.

            – Edward
            Nov 27 '18 at 6:57
















          2














          In the doCatch() Camel moves the exception into a property on the exchange with the key Exchange.EXCEPTION_CAUGHT (http://camel.apache.org/why-is-the-exception-null-when-i-use-onexception.html).



          So you can use



          e.getOut().setBody(new ResponseEntity<String>(e.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class).getMessage(), HttpStatus.OK));





          share|improve this answer
























          • +1 for the answer, but I chose @Bedla's answer because it's more complete and provides multiple solutions to the problem.

            – Edward
            Nov 27 '18 at 6:57














          2












          2








          2







          In the doCatch() Camel moves the exception into a property on the exchange with the key Exchange.EXCEPTION_CAUGHT (http://camel.apache.org/why-is-the-exception-null-when-i-use-onexception.html).



          So you can use



          e.getOut().setBody(new ResponseEntity<String>(e.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class).getMessage(), HttpStatus.OK));





          share|improve this answer













          In the doCatch() Camel moves the exception into a property on the exchange with the key Exchange.EXCEPTION_CAUGHT (http://camel.apache.org/why-is-the-exception-null-when-i-use-onexception.html).



          So you can use



          e.getOut().setBody(new ResponseEntity<String>(e.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class).getMessage(), HttpStatus.OK));






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 20:10









          pcoatespcoates

          1,0551210




          1,0551210













          • +1 for the answer, but I chose @Bedla's answer because it's more complete and provides multiple solutions to the problem.

            – Edward
            Nov 27 '18 at 6:57



















          • +1 for the answer, but I chose @Bedla's answer because it's more complete and provides multiple solutions to the problem.

            – Edward
            Nov 27 '18 at 6:57

















          +1 for the answer, but I chose @Bedla's answer because it's more complete and provides multiple solutions to the problem.

          – Edward
          Nov 27 '18 at 6:57





          +1 for the answer, but I chose @Bedla's answer because it's more complete and provides multiple solutions to the problem.

          – Edward
          Nov 27 '18 at 6:57


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53483879%2fapache-camel-how-to-get-the-exception-message-inside-the-docatch-block%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen