From: Kai Moritz Date: Sat, 19 Sep 2020 10:46:52 +0000 (+0200) Subject: TODO: An neuen Fehler anpassen, Separates Fehler-Template weg? Mehr...? X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=55815784054a9482c2de97cd027c83277e60d225;p=demos%2Ftesting TODO: An neuen Fehler anpassen, Separates Fehler-Template weg? Mehr...? 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. --- diff --git a/src/main/java/de/juplo/demo/ExampleController.java b/src/main/java/de/juplo/demo/ExampleController.java index e16b147..f18f892 100644 --- a/src/main/java/de/juplo/demo/ExampleController.java +++ b/src/main/java/de/juplo/demo/ExampleController.java @@ -5,10 +5,9 @@ import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.DispatcherServlet; +import org.springframework.web.servlet.ModelAndView; import org.thymeleaf.exceptions.TemplateInputException; import java.util.Optional; @@ -43,11 +42,40 @@ public class ExampleController return "view"; } + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler(IllegalArgumentException.class) + public ModelAndView illegalArgumentException(IllegalArgumentException e) + { + LOG.error("{}: {}", HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage()); + ModelAndView mav = new ModelAndView("resolved"); + mav.addObject("exception", e); + return mav; + } + + /** + * This {@link ExceptionHandler @ExceptionHander} is never triggered, + * because the exception is not thrown inside the controller: + * It is functionless! + *

+ * The exception is thrown by Thymeleaf, which is called by the + * {@link DispatcherServlet} after the controller has finished its + * work during the rendering of the outcome. + *

+ *

+ * {@link ExceptionHandler @ExceptionHander's} are not able, to catch + * and resolve exceptions, which are thrown outside of the scope of the + * controller. + * This is also true for {@link ControllerAdvice}. + *

+ */ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(TemplateInputException.class) - public void templateInputException(TemplateInputException e) + public ModelAndView templateInputException(TemplateInputException e) { LOG.error("{}: {}", HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage()); + ModelAndView mav = new ModelAndView("resolved"); + 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/resolved.html b/src/main/resources/templates/resolved.html new file mode 100644 index 0000000..32f64f8 --- /dev/null +++ b/src/main/resources/templates/resolved.html @@ -0,0 +1,14 @@ + + + + Testing Exception-Handling - Template for 503 + + + +

Template for 503

+
+

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 b7891e4..b450f17 100644 --- a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java +++ b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java @@ -86,4 +86,11 @@ class ExceptionHandlingApplicationTests { .perform(get(URI.create("http://FOO/?answer=bar"))) .andExpect(status().isBadRequest()); } + + @Test + void test503() throws Exception { + mvc + .perform(get(URI.create("http://FOO/?template=boom"))) + .andExpect(status().isInternalServerError()); + } }