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

com.github.gpor0.jaffas.exceptions.mappers.ConstraintViolationExceptionMapper Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
package com.github.gpor0.jaffas.exceptions.mappers;

import com.github.gpor0.jaffas.exceptions.model.ApiFaultDetails;

import javax.enterprise.context.ApplicationScoped;
import javax.validation.ConstraintViolationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Provider
@ApplicationScoped
public class ConstraintViolationExceptionMapper extends BaseExceptionMapper implements ExceptionMapper {

    @Override
    public Response toResponse(ConstraintViolationException e) {

        String faultCode = "error.invalidParam";

        ApiFaultDetails faultDetails = new ApiFaultDetails();
        faultDetails.setFaultCode(faultCode);

        Map fieldMap = new HashMap<>();
        e.getConstraintViolations().forEach(v -> {
            String fields[] = v.getPropertyPath().toString().split("\\.");
            if (fields.length > 2)
                fields = Arrays.copyOfRange(fields, 2, fields.length);
            fieldMap.put(String.join(".", fields), String.valueOf(v.getInvalidValue()));
            LOG.error(v.toString());
        });

        faultDetails.setFields(fieldMap);

        LOG.debug(e.getMessage(), e);

        return toResponse(Response.Status.BAD_REQUEST, Arrays.asList(faultDetails), e);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy