1 Reply Latest reply on Jul 29, 2015 6:05 AM by maciavelli

    Exception handling using Switchyard

    horyna

      Hi,

       

      solving problem with handling Business exception (RuntimeException). If in WS request processing is Business exception throw "something" should react for this and return correct but customizer response. My idea is dome handler where i tell "For this type of request, this type of exception return this code". How to solve this using SW?

        • 1. Re: Exception handling using Switchyard
          maciavelli

          Hello, Pavel. If I correctly understand you can do that in your camel Route by catching required type of Exceptions.

              public void configure() {
                  onException(Exception.class)
                          .handled(true)
                          .to("direct://createRsp")
                          .end();
          

          Then you can customize response depending on Exception:

             from("direct://createRsp")
                          .process(
                                  exchange -> {
                                      Response r = new Response<SubscribeRspParams>();
                                      SubscribeRspParams prm = new SubscribeRspParams();
                                      Exception caused = exchange.getProperty(
                                              exchange.EXCEPTION_CAUGHT, Exception.class);
                                      if (caused != null) {
                                          prm.setResponseCode("-1");
                                          prm.setStatus(caused.toString());
                                          log.error("error: " + caused);
                                      } else {
                                          prm.setResponseCode("00");
                                          prm.setStatus("OK");
                                      }
                                      SubscribeRsp rsp = new SubscribeRsp();
                                      r.setStan(stan);
                                      r.setProject(project);
                                      r.setName(name);
                                      r.setParameters(prm);
                                      rsp.setMethod(r);
                                      SwitchYardMessage out = new SwitchYardMessage();
                                      out.setBody(rsp);
                                      exchange.setOut(out);
                                      log.info("Output message: " +exchange.getOut().getBody(String.class) );
                                  });