package io.kluska.bsc.forms.exception.handling.handler;

import io.kluska.bsc.forms.exception.handling.error.ErrorInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
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.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Mateusz Kluska
 */
@ControllerAdvice
@Slf4j
public class RestExceptionHandler extends ResponseEntityExceptionHandler {

    @Override
    protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        log.error("Handled MethodArgumentNotValidException: ", ex);
        BindingResult result = ex.getBindingResult();
        final List<FieldError> fieldErrors = result.getFieldErrors();
        Map<String, String> fieldMessageMap = fieldErrors.stream()
                .collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage,
                        (v1, v2) -> v1 + " & " + v2));

        return new ResponseEntity<>(fieldMessageMap, HttpStatus.BAD_REQUEST);
    }

    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler({Throwable.class})
    @ResponseBody
    public ErrorInfo handleOtherException(Exception ex) {
        return handleException(ex);
    }

    protected ErrorInfo handleException(Throwable ex) {
        log.error(ex.getMessage(), ex);
        return new ErrorInfo(ex, ex.getMessage());
    }
}
@@ -322,29 +322,26 @@
 idation.
-BindingResult
+FieldError
 ;%0Aimport
@@ -364,37 +364,38 @@
 work.validation.
-Field
+Object
 Error;%0Aimport or
@@ -878,16 +878,21 @@
 va.util.
+Array
 List;%0Aim
@@ -910,47 +910,12 @@
 til.
-Map;%0Aimport java.util.stream.Collectors
+List
 ;%0A%0A/
@@ -1238,189 +1238,247 @@
     
-log.error(%22Handled MethodArgumentNotValidException: %22, ex);%0A        BindingResult result = ex.getBindingResult();%0A        final List%3CFieldError%3E fieldErrors = result.getFieldErrors(
+List%3CFieldError%3E fieldErrors = ex.getBindingResult().getFieldErrors();%0A        List%3CObjectError%3E globalErrors = ex.getBindingResult().getGlobalErrors();%0A        List%3CString%3E errors = new ArrayList%3C%3E(fieldErrors.size() + globalErrors.size()
 );%0A 
@@ -1488,110 +1488,95 @@
     
-Map%3C
 String
-, String%3E fieldMessageMap = fieldErrors.stream()%0A                .collect(Collectors.toMap(F
+ error;%0A        for (FieldError fieldError : fieldErrors) %7B%0A            error = f
 ield
@@ -1584,18 +1584,17 @@
 rror
-::
+.
 getField
 , Fi
@@ -1593,11 +1593,21 @@
 ield
-, F
+() + %22: %22 + f
 ield
@@ -1615,10 +1615,9 @@
 rror
-::
+.
 getD
@@ -1629,17 +1629,19 @@
 tMessage
-,
+();
 %0A       
@@ -1649,127 +1649,352 @@
     
+errors.add(error);%0A
        
+ %7D%0A
      
-(v1, v2) -%3E v1
+   for (ObjectError objectError : globalErrors) %7B%0A            error = objectError.getObjectName()
  + %22
- &
+:
  %22 + 
-v2));%0A%0A        return new ResponseEntity%3C%3E(fieldMessageMap, HttpStatus.BAD_REQUEST
+objectError.getDefaultMessage();%0A            errors.add(error);%0A        %7D%0A        return handleExceptionInternal(ex, new ErrorInfo(ex, String.join(%22, %22, errors)),%0A                headers, status, request
 );%0A 

Change MethodArgumentNotValidException handling.