Marcin Erbel
Marcin Erbel

Reputation: 1643

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? :)

Upvotes: 3

Views: 361

Answers (3)

Marcin Erbel
Marcin Erbel

Reputation: 1643

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

Upvotes: 1

kjhughes
kjhughes

Reputation: 111696

That's a SAX error message, and it appears to be quite clearly stated, but see ErrorHandler and DefaultHandler to customize it however you'd prefer.

Upvotes: 1

Florent Georges
Florent Georges

Reputation: 2327

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

Upvotes: 1

Related Questions