3 Replies Latest reply on Sep 23, 2015 10:08 PM by igarashitm

    Catching validation exception

    maciavelli

      Hello. I am using XML based validator which if validation fails throws Exception:

      Caused by: org.switchyard.HandlerException: SWITCHYARD014000: Validator 'org.switchyard.validate.xml.internal.XmlValidator' failed: 1 validation error(s):
      org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'threshold'. One of '{amount}' is expected.
      

      I am using XML validation for my REST service. In case of Validation Exception I get "500 Internal Server Error". But my goal is to handle Exception and send back formatted response.

      Below is my switchYard diagram:

      switchyard.jpg

      I tried Exception handling in my Camel Route(SubscribeRoute on image above) but without any success.

      Could you help me, how can I handle SwitchYard`s Validation Exceptions?

        • 1. Re: Catching validation exception
          trohovsky

          Hi Kuat,

           

          it's not possible to do error handling in SubscribeRoute component in this case, because validation is done before the massage reaches the component. Creating of a custom message composer (Message Composition - SwitchYard - Project Documentation Editor) could be the way how to do it.

           

          Cheers,

          Tomas

          • 2. Re: Catching validation exception
            maciavelli

            Thank you, Tomas. Yeah, your post helped.

            And below is my code, may be it helps to some developers:

            public class SubscribeComposer extends RESTEasyMessageComposer {
            
            
                private Logger LOG = LoggerFactory.getLogger(this.getClass().getName());
                public String stan;
                public String project;
                public String name;
            
            
                @Override
                public Message compose(RESTEasyBindingData source, Exchange exchange) throws Exception {
                    Message target = super.compose(source, exchange);
                    kz.atf.types.SubscribeRqu rqu = target.getContent(kz.atf.types.SubscribeRqu.class);
                    stan = rqu.getMethod().getStan();
                    project = rqu.getMethod().getProject();
                    name = rqu.getMethod().getName();
                    return target;
                }
            
            
                @Override
                public RESTEasyBindingData decompose(Exchange exchange, RESTEasyBindingData target) throws Exception {
                    LOG.info("state: " + exchange.getState());
                    kz.atf.types.common.Response r = new kz.atf.types.common.Response<SubscribeRspParams>();
                    SubscribeRspParams prm = new SubscribeRspParams();
                    SubscribeRsp rsp = new SubscribeRsp();
                    r.setStan(stan);
                    r.setProject(project);
                    r.setName(name);
                    if (exchange.getContext().getProperty("exCaused") != null) {
                        prm.setResponseCode("-1");
                        prm.setStatus((String) exchange.getContext().getProperty("exCaused").getValue());
                    } else {
                        if (exchange.getState().equals(ExchangeState.FAULT)) {
                            if (exchange.getMessage().getContent() instanceof HandlerException) {
                                if (exchange.getMessage().getContent(String.class).contains("XmlValidator")) {
                                    HandlerException he = exchange.getMessage().getContent(HandlerException.class);
                                    String message = he.getMessage().substring(he.getMessage().indexOf("SAXParseException"), he.getMessage().length());
                                    prm.setResponseCode("-1");
                                    prm.setStatus(message);
                                    r.setParameters(prm);
                                    rsp.setMethod(r);
                                    throw new WebApplicationException(
                                            Response
                                            .status(Response.Status.FORBIDDEN)
                                            .entity(rsp)
                                            .type(MediaType.TEXT_XML)
                                            .build());
                                }
                            }
                        } else {
                            prm.setResponseCode("00");
                            prm.setStatus("OK");
                        }
                    }
                    r.setParameters(prm);
                    rsp.setMethod(r);
                    exchange.getMessage().setContent(rsp);
                    target = super.decompose(exchange, target);
                    return target;
                }
            }
            
            • 3. Re: Catching validation exception
              igarashitm

              The another option would be to invoke XmlValidator programatically as opposed to use it in a declarative way. You can get a ValidationResult object directly in that way.