package org.synyx.urlaubsverwaltung.web;

import org.slf4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import static java.lang.invoke.MethodHandles.lookup;
import static org.slf4j.LoggerFactory.getLogger;


/**
 * Handles exceptions and redirects to error page.
 */
@ControllerAdvice(annotations = Controller.class)
public class ExceptionHandlerControllerAdvice {

    private static final Logger LOG = getLogger(lookup().lookupClass());

    private static final String ERROR_PAGE_NAME = "errors";

    /**
     * Get the common error page.
     *
     * @param exception has information about cause of error
     * @return the error page as {@link ModelAndView}
     */
    private static ModelAndView getErrorPage(Exception exception, HttpStatus httpStatus) {

        ModelAndView modelAndView = new ModelAndView(ERROR_PAGE_NAME);
        modelAndView.addObject("exception", exception);
        modelAndView.addObject("statusCode", httpStatus.value());

        return modelAndView;
    }


    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(
        {AbstractNoResultFoundException.class, NumberFormatException.class}
    )
    public ModelAndView handleException(AbstractNoResultFoundException exception) {

        if (LOG.isDebugEnabled()) {
            LOG.debug("An exception was thrown", exception);
        }
        return ExceptionHandlerControllerAdvice.getErrorPage(exception, HttpStatus.BAD_REQUEST);
    }


    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler(AccessDeniedException.class)
    public ModelAndView handleException(AccessDeniedException exception) {

        if (LOG.isDebugEnabled()) {
            LOG.debug("An exception was thrown", exception);
        }
        return ExceptionHandlerControllerAdvice.getErrorPage(exception, HttpStatus.FORBIDDEN);
    }


    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ModelAndView handleException(Exception exception) {

        LOG.warn("An exception was thrown", exception);

        return ExceptionHandlerControllerAdvice.getErrorPage(exception, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

@@ -569,16 +569,213 @@
 Logger;%0A
+import static org.springframework.http.HttpStatus.BAD_REQUEST;%0Aimport static org.springframework.http.HttpStatus.FORBIDDEN;%0Aimport static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;%0A
 %0A%0A/**%0A *
@@ -890,16 +890,20 @@
 c class 
+View
 Exceptio
@@ -1580,27 +1580,16 @@
 eStatus(
-HttpStatus.
 BAD_REQU
@@ -1615,25 +1615,16 @@
 Handler(
-%0A        
 %7BAbstrac
@@ -1888,32 +1888,36 @@
 %0A        return 
+View
 ExceptionHandler
@@ -1957,27 +1957,16 @@
 eption, 
-HttpStatus.
 BAD_REQU
@@ -1999,27 +1999,16 @@
 eStatus(
-HttpStatus.
 FORBIDDE
@@ -2251,32 +2251,36 @@
 %0A        return 
+View
 ExceptionHandler
@@ -2320,27 +2320,16 @@
 eption, 
-HttpStatus.
 FORBIDDE
@@ -2360,27 +2360,16 @@
 eStatus(
-HttpStatus.
 INTERNAL
@@ -2558,16 +2558,20 @@
  return 
+View
 Exceptio
@@ -2619,27 +2619,16 @@
 eption, 
-HttpStatus.
 INTERNAL

Improve readability