package org.gbif.ws.server.mapper;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.validation.ConstraintViolationException;
import java.util.Comparator;

/**
 * Converts validation exceptions into a http 422 bad request and gives a meaningful messages on the issues.
 */
@ControllerAdvice
public class ValidationExceptionMapper {

  private static final Logger LOG = LoggerFactory.getLogger(ValidationExceptionMapper.class);

  @ExceptionHandler({MethodArgumentNotValidException.class, ConstraintViolationException.class})
  public ResponseEntity<Object> toResponse(MethodArgumentNotValidException exception) {
    LOG.error(exception.getMessage(), exception);
    ImmutableList.Builder<String> builder = ImmutableList.builder();

    exception.getBindingResult()
        .getAllErrors()
        .stream()
        .map(error -> ((FieldError) error))
        .sorted(Comparator.comparing(FieldError::getField, Comparator.naturalOrder()))
        .forEach(error -> {
          LOG.debug("Validation of [{}] failed: {}", error.getField(), error.getDefaultMessage());

          builder.add(String.format("Validation of [%s] failed: %s", error.getField(), error.getDefaultMessage()));
        });

    return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY)
        .contentType(MediaType.TEXT_PLAIN)
        .body("<ul><li>" + Joiner.on("</li><li>").join(builder.build()) + "</li></ul>");
  }
}

@@ -115,16 +115,61 @@
 leList;%0A
+import javax.validation.ConstraintViolation;%0A
 import o
@@ -978,17 +978,16 @@
 Handler(
-%7B
 MethodAr
@@ -1019,45 +1019,8 @@
 lass
-, ConstraintViolationException.class%7D
 )%0A  
@@ -1155,16 +1155,17 @@
 ption);%0A
+%0A
     Immu
@@ -1559,17 +1559,16 @@
 age());%0A
-%0A
         
@@ -1684,24 +1684,767 @@
        %7D);%0A%0A
+    return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY)%0A        .contentType(MediaType.TEXT_PLAIN)%0A        .body(%22%3Cul%3E%3Cli%3E%22 + Joiner.on(%22%3C/li%3E%3Cli%3E%22).join(builder.build()) + %22%3C/li%3E%3C/ul%3E%22);%0A  %7D%0A%0A  @ExceptionHandler(ConstraintViolationException.class)%0A  public ResponseEntity%3CObject%3E toResponse(ConstraintViolationException exception) %7B%0A    LOG.error(exception.getMessage(), exception);%0A%0A    ImmutableList.Builder%3CString%3E builder = ImmutableList.builder();%0A%0A    for (ConstraintViolation%3C?%3E cv : exception.getConstraintViolations()) %7B%0A      LOG.debug(%22Validation of %5B%7B%7D%5D failed: %7B%7D%22, cv.getPropertyPath(), cv.getMessage());%0A      builder.add(String.format(%22Validation of %5B%25s%5D failed: %25s%22, cv.getPropertyPath(), cv.getMessage()));%0A    %7D%0A%0A
     return R

Fix ValidationExceptionMapper