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

io.fluxcapacitor.javaclient.tracking.handling.validation.ValidationException Maven / Gradle / Ivy

There is a newer version: 0.1015.0
Show newest version
package io.fluxcapacitor.javaclient.tracking.handling.validation;

import lombok.Getter;

import javax.validation.ConstraintViolation;
import java.beans.ConstructorProperties;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toList;

@Getter
public class ValidationException extends RuntimeException {

    private final SortedSet violations;

    public ValidationException(Set> violations) {
        super(String.format("One or more constraints were violated:%s%s", System.lineSeparator(), format(violations)));
        this.violations = violations.stream().map(ValidationException::format).collect(toCollection(TreeSet::new));
    }

    @ConstructorProperties({"message", "violations"})
    public ValidationException(String message, Set violations) {
        super(message);
        this.violations = new TreeSet<>(violations);
    }

    protected static String format(ConstraintViolation v) {
        return String.format("property %s in class %s %s", v.getPropertyPath(), v.getRootBeanClass().getSimpleName(),
                             v.getMessage());
    }

    protected static String format(Set> violations) {
        return String.join(System.lineSeparator(), violations.stream().map(
                ValidationException::format).sorted().collect(toList()));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy