From 2e733c78dc0648883a7e589e39c92972ff2c0198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Theodor=20Angerg=C3=A5rd?= Date: Wed, 13 Nov 2024 21:27:45 +0100 Subject: [PATCH] Add logging and handler for IllegalArgumentException (#930) --- .../gamma/adapter/primary/web/ThymeleafAdvice.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/src/main/java/it/chalmers/gamma/adapter/primary/web/ThymeleafAdvice.java b/app/src/main/java/it/chalmers/gamma/adapter/primary/web/ThymeleafAdvice.java index 828908d46..e4912730d 100644 --- a/app/src/main/java/it/chalmers/gamma/adapter/primary/web/ThymeleafAdvice.java +++ b/app/src/main/java/it/chalmers/gamma/adapter/primary/web/ThymeleafAdvice.java @@ -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; @@ -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() @@ -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"));