Skip to content

Commit 2e733c7

Browse files
authored
Add logging and handler for IllegalArgumentException (#930)
1 parent f242747 commit 2e733c7

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

app/src/main/java/it/chalmers/gamma/adapter/primary/web/ThymeleafAdvice.java

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import jakarta.servlet.http.HttpServletRequest;
77
import jakarta.servlet.http.HttpServletResponse;
88
import java.io.IOException;
9+
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
912
import org.springframework.http.HttpStatus;
1013
import org.springframework.security.access.AccessDeniedException;
1114
import org.springframework.web.bind.annotation.ControllerAdvice;
@@ -18,6 +21,8 @@
1821
@ControllerAdvice
1922
public class ThymeleafAdvice {
2023

24+
private static final Logger LOGGER = LoggerFactory.getLogger(ThymeleafAdvice.class);
25+
2126
@ModelAttribute("isAdmin")
2227
public boolean isAdmin() {
2328
if (AuthenticationExtractor.getAuthentication()
@@ -28,6 +33,12 @@ public boolean isAdmin() {
2833
}
2934
}
3035

36+
@ExceptionHandler(IllegalArgumentException.class)
37+
public ModelAndView handleIllegalArgumentException(HttpServletRequest request, IllegalArgumentException ex) {
38+
LOGGER.error("Caught IllegalArgumentException. This shouldn't happen as validators should catch these issues:", ex);
39+
return new ModelAndView("redirect:/error");
40+
}
41+
3142
@ExceptionHandler(AccessGuard.AccessDeniedException.class)
3243
public ModelAndView handleAccessDeniedException(HttpServletRequest request) {
3344
boolean htmxRequest = "true".equals(request.getHeader("HX-Request"));

0 commit comments

Comments
 (0)