Skip to content

Commit

Permalink
Add logging and handler for IllegalArgumentException (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
Portals authored Nov 13, 2024
1 parent f242747 commit 2e733c7
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
Expand All @@ -18,6 +21,8 @@
@ControllerAdvice
public class ThymeleafAdvice {

private static final Logger LOGGER = LoggerFactory.getLogger(ThymeleafAdvice.class);

@ModelAttribute("isAdmin")
public boolean isAdmin() {
if (AuthenticationExtractor.getAuthentication()
Expand All @@ -28,6 +33,12 @@ public boolean isAdmin() {
}
}

@ExceptionHandler(IllegalArgumentException.class)
public ModelAndView handleIllegalArgumentException(HttpServletRequest request, IllegalArgumentException ex) {
LOGGER.error("Caught IllegalArgumentException. This shouldn't happen as validators should catch these issues:", ex);
return new ModelAndView("redirect:/error");
}

@ExceptionHandler(AccessGuard.AccessDeniedException.class)
public ModelAndView handleAccessDeniedException(HttpServletRequest request) {
boolean htmxRequest = "true".equals(request.getHeader("HX-Request"));
Expand Down

0 comments on commit 2e733c7

Please sign in to comment.