简体   繁体   English

用于架构验证错误的自定义映射器

[英]Custom mapper for schema validation errors

I've used camel validator and I'm catching errors from the schema validation like a : 我使用了camel验证器 ,我从模式验证中捕获错误,如:

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. Saxon非常擅长错误报告。 Its validator gives you understandable messages in the first place. 它的验证器首先为您提供可理解的消息。

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

I've created validation with xsd through camel validation component: 我已经通过camel验证组件使用xsd创建了验证:

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

then I've used doCatch inside doTry block to catch exception: 然后我在doTry块中使用了doCatch来捕获异常:

<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 :) 之后我写了自定义Camel处理器,它很棒:)

    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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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