io.fluxcapacitor.javaclient.tracking.handling.validation.ValidationException Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-client Show documentation
Show all versions of java-client Show documentation
Default Java client library for interfacing with Flux Capacitor.
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()));
}
}