use of org.qi4j.library.rest.server.restlet.ConstraintViolationMessages in project qi4j-sdk by Qi4j.
the class ContextResource method handleException.
private void handleException(Response response, Throwable ex) {
while (ex instanceof InvocationTargetException) {
ex = ex.getCause();
}
try {
throw ex;
} catch (ResourceException e) {
// IAE (or subclasses) are considered client faults
response.setEntity(new StringRepresentation(e.getMessage()));
response.setStatus(e.getStatus());
} catch (ConstraintViolationException e) {
try {
ConstraintViolationMessages cvm = new ConstraintViolationMessages();
// CVE are considered client faults
String messages = "";
Locale locale = ObjectSelection.type(Locale.class);
for (ConstraintViolation constraintViolation : e.constraintViolations()) {
if (!messages.isEmpty()) {
messages += "\n";
}
messages += cvm.getMessage(constraintViolation, locale);
}
response.setEntity(new StringRepresentation(messages));
response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
} catch (Exception e1) {
response.setEntity(new StringRepresentation(e.getMessage()));
response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
}
} catch (IllegalArgumentException e) {
// IAE (or subclasses) are considered client faults
response.setEntity(new StringRepresentation(e.getMessage()));
response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
} catch (RuntimeException e) {
// RuntimeExceptions are considered server faults
LoggerFactory.getLogger(getClass()).warn("Exception thrown during processing", e);
response.setEntity(new StringRepresentation(e.getMessage()));
response.setStatus(Status.SERVER_ERROR_INTERNAL);
} catch (Exception e) {
// Checked exceptions are considered client faults
String s = e.getMessage();
if (s == null) {
s = e.getClass().getSimpleName();
}
response.setEntity(new StringRepresentation(s));
response.setStatus(Status.CLIENT_ERROR_UNPROCESSABLE_ENTITY);
} catch (Throwable e) {
// Anything else are considered server faults
LoggerFactory.getLogger(getClass()).error("Exception thrown during processing", e);
response.setEntity(new StringRepresentation(e.getMessage()));
response.setStatus(Status.SERVER_ERROR_INTERNAL);
}
}
Aggregations