package eionet.webq.web.controller.util;

import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class ExceptionHandlerAdvice {

    private static final Logger LOGGER = Logger.getLogger("webq2");
    private static final String EXCEPTION_STATUS = "status";
    private static final String EXCEPTION_MESSAGE = "errorMessage";
    public static final String ERROR_VIEW = "/error";

    @ExceptionHandler(Exception.class)
    public ModelAndView genericExceptionHandler(Exception exception) {
        LOGGER.error("An error has occurred", exception);
        ModelAndView modelAndView = new ModelAndView(ERROR_VIEW);
        modelAndView.setStatus(HttpStatus.INTERNAL_SERVER_ERROR);
        modelAndView.addObject(EXCEPTION_STATUS, HttpStatus.INTERNAL_SERVER_ERROR.value());
        modelAndView.addObject(EXCEPTION_MESSAGE, exception.getMessage());
        return modelAndView;
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ModelAndView maxUploadSizeExceptionHandler(MaxUploadSizeExceededException exception) {
        LOGGER.warn(exception);
        ModelAndView modelAndView = new ModelAndView(ERROR_VIEW);
        modelAndView.setStatus(HttpStatus.BAD_REQUEST);
        modelAndView.addObject(EXCEPTION_STATUS, HttpStatus.BAD_REQUEST.value());
        String exceptionMessage = String.format("Maximum size of file exceeded. Allowed size is %d megabytes", (exception.getMaxUploadSize() / 1024 / 1024));
        modelAndView.addObject(EXCEPTION_MESSAGE, exceptionMessage);
        return modelAndView;
    }
}

@@ -63,24 +63,84 @@
 g4j.Logger;%0A
+import org.springframework.core.annotation.AnnotationUtils;%0A
 import org.s
@@ -297,24 +297,87 @@
 ionHandler;%0A
+import org.springframework.web.bind.annotation.ResponseStatus;%0A
 import org.s
@@ -750,21 +750,22 @@
 %22;%0A    p
-ublic
+rivate
  static 
@@ -961,32 +961,289 @@
 d%22, exception);%0A
+        ResponseStatus responseStatusAnnotation = AnnotationUtils.findAnnotation(exception.getClass(), ResponseStatus.class);%0A        HttpStatus status = responseStatusAnnotation != null ? responseStatusAnnotation.code() : HttpStatus.INTERNAL_SERVER_ERROR;%0A
         ModelAnd
@@ -1327,40 +1327,14 @@
 tus(
-HttpStatus.INTERNAL_SERVER_ERROR
+status
 );%0A 
@@ -1385,40 +1385,14 @@
 US, 
-HttpStatus.INTERNAL_SERVER_ERROR
+status
 .val

Fix handler to rethrow ResponseStatus annotated exceptions