Search in sources :

Example 1 with ConstraintViolationMessages

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);
    }
}
Also used : Locale(java.util.Locale) StringRepresentation(org.restlet.representation.StringRepresentation) ConstraintViolation(org.qi4j.api.constraint.ConstraintViolation) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException) ConstraintViolationMessages(org.qi4j.library.rest.server.restlet.ConstraintViolationMessages) ResourceException(org.restlet.resource.ResourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EntityTypeNotFoundException(org.qi4j.api.unitofwork.EntityTypeNotFoundException) ResourceException(org.restlet.resource.ResourceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) ConstraintViolationException(org.qi4j.api.constraint.ConstraintViolationException)

Aggregations

UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Locale (java.util.Locale)1 ConstraintViolation (org.qi4j.api.constraint.ConstraintViolation)1 ConstraintViolationException (org.qi4j.api.constraint.ConstraintViolationException)1 EntityTypeNotFoundException (org.qi4j.api.unitofwork.EntityTypeNotFoundException)1 NoSuchEntityException (org.qi4j.api.unitofwork.NoSuchEntityException)1 ConstraintViolationMessages (org.qi4j.library.rest.server.restlet.ConstraintViolationMessages)1 StringRepresentation (org.restlet.representation.StringRepresentation)1 ResourceException (org.restlet.resource.ResourceException)1