简体   繁体   中英

Custom mapper for schema validation errors

I've used camel validator and I'm catching errors from the schema validation like a :

org.xml.sax.SAXParseException: cvc-minLength-valid: Value '' with length = '0' is not facet-valid with respect to minLength '1' for type

Is it any tool which will be good to map this errors for a prettier statements? I can always just iterate on the erros, split on them and prepare custom mapper, but maybe there is sth better than this? :)

Saxon is really good at error reporting. Its validator gives you understandable messages in the first place.

这是一个SAX错误消息,它看起来非常明确,但请参阅ErrorHandlerDefaultHandler来自定义它,但您更喜欢。

I've created validation with xsd through camel validation component:

<to uri="validator:xsd/myValidator.xsd"/>

then I've used doCatch inside doTry block to catch exception:

<doCatch>
    <exception>org.apache.camel.ValidationException</exception>
    <log message="catch exception ${body}" loggingLevel="ERROR" />
    <process ref="schemaErrorHandler"/>
</doCatch>

After that I wrote custom Camel Processor and it works great :)

    public class SchemaErrorHandler implements Processor {

    private final String STATUS_CODE = "6103";

    private final String SEVERITY_CODE = "2";

    @Override
    public void process(Exchange exchange) throws Exception {

        Map<String, Object> map = exchange.getProperties();
        String statusDesc = "Unknown exception";
        if (map != null) {
            SchemaValidationException exception = (SchemaValidationException) map.get("CamelExceptionCaught");
            if (exception != null && !CollectionUtils.isEmpty(exception.getErrors())) {
                StringBuffer buffer = new StringBuffer();
                for (SAXParseException e : exception.getErrors()) {
                    statusDesc = e.getMessage();
                    buffer.append(statusDesc);
                }
                statusDesc = buffer.toString();
            }
        }
        Fault fault = new Fault(new Message(statusDesc, (ResourceBundle) null));
        fault.setDetail(ErrorUtils.createDetailSection(STATUS_CODE, statusDesc, exchange, SEVERITY_CODE));
        throw fault;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM