架构验证错误的自定义映射程序



我使用了骆驼式验证器,我正在从模式验证中捕获错误,比如:

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

有什么工具可以将这些错误映射为更漂亮的语句吗?我总是可以迭代错误,对它们进行拆分,并准备自定义映射程序,但也许还有比这更好的东西?:)

Saxon非常擅长错误报告。它的验证器首先为您提供可理解的消息。

这是一条SAX错误消息,它似乎已经非常清楚地说明了,但请参阅ErrorHandler和DefaultHandler以自定义它。

我已经通过骆驼式验证组件用xsd创建了验证

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

然后我在doTry块中使用了doCatch来捕获异常:

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

在那之后,我写了自定义骆驼处理器,它工作得很好:)

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

最新更新