Skip to content

Commit

Permalink
[cleanup] Improve the message created in case of a validation error
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Bégaudeau <[email protected]>
  • Loading branch information
sbegaudeau committed Aug 19, 2023
1 parent e802c31 commit 0b4faaf
Showing 1 changed file with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

package com.svalyn.studio.domain;

import org.springframework.data.domain.AbstractAggregateRoot;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Validation;
import jakarta.validation.ValidatorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.annotation.Transient;
import org.springframework.data.domain.AbstractAggregateRoot;

import java.util.stream.Collectors;

/**
Expand All @@ -36,16 +38,24 @@
*/
public class AbstractValidatingAggregateRoot<A extends AbstractValidatingAggregateRoot<A>> extends AbstractAggregateRoot<A> {

@Transient
private final Logger logger = LoggerFactory.getLogger(AbstractValidatingAggregateRoot.class);

@Override
protected <T> T registerEvent(T event) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
var validator = factory.getValidator();
var violations = validator.validate(event);
if (!violations.isEmpty()) {
var message = violations.stream()
.map(ConstraintViolation::getMessage)
.collect(Collectors.joining(", "));
throw new ConstraintViolationException(message, violations);
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
var validator = factory.getValidator();
var violations = validator.validate(event);
if (!violations.isEmpty()) {
var message = violations.stream()
.map(constraintViolation -> constraintViolation.getPropertyPath().toString() + " " + constraintViolation.getMessage())
.collect(Collectors.joining(", "));

var exception = new ConstraintViolationException(message, violations);
logger.warn(exception.getMessage(), exception);

throw exception;
}
}

return super.registerEvent(event);
Expand Down

0 comments on commit 0b4faaf

Please sign in to comment.