Custom mapper for schema validation errors
I used camel camel and I am breaking errors from schema validation like:
org.xml.sax.SAXParseException: cvc-minLength-valid: Value '' with length = '0' is not facet-valid with respect to minLength '1' for type
Is this any tool that would be good at matching these errors for prettier statements? I can always just iterate over eras, divide into them and prepare a custom cartographer, but maybe better? :)
+3
source to share
3 answers
I created validation with xsd via camel validation component:
<to uri="validator:xsd/myValidator.xsd"/>
then i used doCatch inside doTry block to throw exception:
<doCatch>
<exception>org.apache.camel.ValidationException</exception>
<log message="catch exception ${body}" loggingLevel="ERROR" />
<process ref="schemaErrorHandler"/>
</doCatch>
After that I wrote a 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;
}
}
+1
source to share
This is a SAX error message, and it seems to be pretty clear, but see ErrorHandler and DefaultHandler to customize it, but you'd rather.
+1
source to share