From 8e7eebe0d72ed85827ea61b943d49b4cf42b85c1 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 19 Sep 2020 12:46:52 +0200 Subject: [PATCH] Added a working @ExceptionHandler * Both handlers resolve the handled exception as 503 Internal Server Error and are using the template "error/503", which was extended to print a message, that the reported exception was catched and handled. * If one starts the app, it is clearly visible, that the mechanism only works for exceptions, that are thrown in the context of the controller. * Also added a test-case, that shows, that MockMvc shows the expected behaviour for exceptions, that are thrown inside the container. --- .../java/de/juplo/demo/ExampleController.java | 16 +++++++++++++++- .../java/de/juplo/demo/ExampleService.java | 6 ++++++ src/main/resources/templates/400.html | 14 ++++++++++++++ .../ExceptionHandlingApplicationTests.java | 19 +++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/templates/400.html diff --git a/src/main/java/de/juplo/demo/ExampleController.java b/src/main/java/de/juplo/demo/ExampleController.java index de806ef..a6efeb2 100644 --- a/src/main/java/de/juplo/demo/ExampleController.java +++ b/src/main/java/de/juplo/demo/ExampleController.java @@ -7,6 +7,7 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.DispatcherServlet; +import org.springframework.web.servlet.ModelAndView; import org.thymeleaf.exceptions.TemplateProcessingException; import java.util.Optional; @@ -41,6 +42,16 @@ public class ExampleController return "view"; } + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(IllegalArgumentException.class) + public ModelAndView illegalArgumentException(IllegalArgumentException e) + { + LOG.error("{}: {}", HttpStatus.BAD_REQUEST, e.getMessage()); + ModelAndView mav = new ModelAndView("400"); + mav.addObject("exception", e); + return mav; + } + /** * This {@link ExceptionHandler @ExceptionHander} is never triggered, * because the exception is not thrown inside the controller: @@ -59,9 +70,12 @@ public class ExampleController */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(TemplateProcessingException.class) - public void templateInputException(TemplateProcessingException e) + public ModelAndView templateInputException(TemplateProcessingException e) { LOG.error("{}: {}", HttpStatus.BAD_REQUEST, e.getMessage()); + ModelAndView mav = new ModelAndView("400"); + mav.addObject("exception", e); + return mav; } } diff --git a/src/main/java/de/juplo/demo/ExampleService.java b/src/main/java/de/juplo/demo/ExampleService.java index ec21629..45f2ca1 100644 --- a/src/main/java/de/juplo/demo/ExampleService.java +++ b/src/main/java/de/juplo/demo/ExampleService.java @@ -13,8 +13,14 @@ public class ExampleService return Optional.empty(); if (answer == 42) + { return Optional.of(true); + } else + { + if (answer % 7 == 0) + throw new IllegalArgumentException(answer + " is devidable by 7!"); return Optional.of(false); + } } } diff --git a/src/main/resources/templates/400.html b/src/main/resources/templates/400.html new file mode 100644 index 0000000..f1d4d5d --- /dev/null +++ b/src/main/resources/templates/400.html @@ -0,0 +1,14 @@ + + + + Testing Exception-Handling - Template for 400 + + + +

Template for 400

+
+

EXCEPTION

+

Back to HOME

+
+ + diff --git a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java index c9a4d49..62f3a13 100644 --- a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java +++ b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java @@ -140,4 +140,23 @@ class ExceptionHandlingApplicationTests { verify(service, times(0)).checkAnswer(anyInt()); } + + @Test + void test400ForExceptionInBusinessLogic() throws Exception { + when(service.checkAnswer(anyInt())).thenThrow(new IllegalArgumentException("FOO!")); + + mvc + .perform(get(URI.create("http://FOO/?answer=1234"))) + .andExpect(status().isBadRequest()) + .andDo((result) -> { + String content = result.getResponse().getContentAsString(); + Document doc = Jsoup.parse(content); + assertThat(doc.selectFirst("title").text()) + .isEqualTo("400: IllegalArgumentException"); + assertThat(doc.selectFirst("div > p > strong").text()) + .isEqualTo("Catched exception: java.lang.IllegalArgumentException: FOO!"); + }); + + verify(service, times(1)).checkAnswer(anyInt()); + } } -- 2.20.1