I have a Spring MVC application, and I want to have a custom error page for certain errors. For example, 404 and 500.
Now, my application can, in a certain condition, throw an exception that causes a 500 status code. Without the custom error page, it looks like this:
Now I want to have a custom error page, so I apply the following web.xml
file:
<web-app><error-page><error-code>404</error-code><location>/error404</location></error-page><error-page><error-code>400</error-code><location>/error400</location></error-page><error-page><error-code>500</error-code><location>/error500</location></error-page></web-app>
And the following ErrorController
@Controllerpublic class ErrorController { @GetMapping("/error404") public String error404() { return "html/error404"; } @GetMapping("/error500") public String error500() { return "html/error500"; }}
The 404 works great - if I try to browse to http://localhost:8080/my-app/non-existing-url
I get my custom 404 error page.
However, when I cause my application to throw the same exception that caused a 500 to be returned above, I don't get my custom page. Instead, I get:
Can someone help me understand why that is?!
EDIT
I found that when throwing another exception that causes a 500:
then, if I enable the custom error page, I do get my custom error page. So it seems that for specific exceptions it does work but for other it doesn't. What is the logic behind this?