ValidationException.java

  1. package no.nav.data.common.exceptions;

  2. import no.nav.data.common.validator.ValidationError;
  3. import org.springframework.http.HttpStatus;
  4. import org.springframework.web.bind.annotation.ResponseStatus;

  5. import java.util.Collections;
  6. import java.util.List;
  7. import java.util.stream.Collectors;

  8. @ResponseStatus(HttpStatus.BAD_REQUEST)
  9. public class ValidationException extends RuntimeException {

  10.     private final List<ValidationError> validationErrors;

  11.     public ValidationException(String message) {
  12.         this(Collections.emptyList(), message);
  13.     }

  14.     public ValidationException(List<ValidationError> validationErrors, String message) {
  15.         super(message + (validationErrors.isEmpty() ? "" : " " + validationErrors));
  16.         this.validationErrors = validationErrors;
  17.     }

  18.     public List<ValidationError> get() {
  19.         return validationErrors;
  20.     }

  21.     public ValidationError get(String errorType) {
  22.         return validationErrors.stream().filter(v -> v.getErrorType().equals(errorType)).findFirst().orElse(null);
  23.     }

  24.     public String toErrorString() {
  25.         return validationErrors.stream().map(ValidationError::toString).collect(Collectors.joining(", "));
  26.     }
  27. }