All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.agrest.cayenne.provider.ValidationExceptionMapper Maven / Gradle / Ivy

There is a newer version: 5.0.M19
Show newest version
package io.agrest.cayenne.provider;

import io.agrest.SimpleResponse;
import org.apache.cayenne.validation.ValidationException;
import org.apache.cayenne.validation.ValidationResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

/**
 * @since 1.1
 */
@Provider
public class ValidationExceptionMapper implements ExceptionMapper {

    private static final Logger LOGGER = LoggerFactory.getLogger(ValidationExceptionMapper.class);

    private static final String ERROR_MESSAGE_EN = "Object validation failed. There were %s failure(s).";

    @Override
    public Response toResponse(ValidationException exception) {

        ValidationResult validation = exception.getValidationResult();
        Status status = Status.BAD_REQUEST;

        // TODO: perhaps we can convert this in a true DataResponse with a list
        //   of failed properties that can be analyzed on the client?
        // for now log details, return a generic validation message to avoid leaking too much server internals
        LOGGER.info("{} {} ({})", status.getStatusCode(), status.getReasonPhrase(), validation);

        // TODO: localize error message
        String message = String.format(ERROR_MESSAGE_EN, validation.getFailures().size());

        SimpleResponse body = new SimpleResponse(false, message);
        return Response.status(status).entity(body).type(MediaType.APPLICATION_JSON_TYPE).build();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy