package org.gbif.registry.ws.advice;

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 org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class ValidationExceptionMapper {

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

  @ResponseStatus(HttpStatus.BAD_REQUEST)
  @ExceptionHandler(MethodArgumentNotValidException.class)
  public ResponseEntity toResponse(MethodArgumentNotValidException exception) {
    LOG.error(exception.getMessage(), exception);
    ImmutableList.Builder<String> b = ImmutableList.builder();
    exception.getBindingResult().getAllErrors().forEach(error -> {
      LOG.debug("Validation of [{}] failed: {}",
        ((FieldError) error).getField(), error.getDefaultMessage());
      b.add(String.format("Validation of [%s] failed: %s",
        ((FieldError) error).getField(), error.getDefaultMessage()));
    });
    return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY)
      .contentType(MediaType.TEXT_PLAIN)
      .body("<ul><li>" + Joiner.on("</li><li>").join(b.build()) + "</li></ul>");
  }
}

@@ -622,16 +622,46 @@
 tatus;%0A%0A
+import java.util.Comparator;%0A%0A
 @Control
@@ -1073,16 +1073,22 @@
 tring%3E b
+uilder
  = Immut
@@ -1107,16 +1107,17 @@
 lder();%0A
+%0A
     exce
@@ -1140,16 +1140,23 @@
 Result()
+%0A      
 .getAllE
@@ -1162,16 +1162,166 @@
 Errors()
+%0A      .stream()%0A      .map(error -%3E ((FieldError) error))%0A      .sorted(Comparator.comparing(FieldError::getField, Comparator.naturalOrder()))%0A      
 .forEach
@@ -1332,16 +1332,18 @@
 or -%3E %7B%0A
+  
       LO
@@ -1386,37 +1386,14 @@
 %7B%7D%22,
-%0A        ((FieldError)
  error
-)
 .get
@@ -1433,15 +1433,24 @@
 ));%0A
+%0A  
       b
+uilder
 .add
@@ -1500,37 +1500,14 @@
 %25s%22,
-%0A        ((FieldError) error)
+ error
 .get
@@ -1552,12 +1552,15 @@
     
+  
 %7D);%0A
+%0A
     
@@ -1662,16 +1662,16 @@
 _PLAIN)%0A
-
       .b
@@ -1716,16 +1716,22 @@
 ).join(b
+uilder
 .build()

Use fixed order of exceptions in ValidationExceptionMapper