|
9 | 9 |
|
10 | 10 | @Controller |
11 | 11 | public class ErrorController { |
12 | | - |
13 | | - @RequestMapping(value = "500Error" , method = RequestMethod.GET) |
14 | | - public void throwRuntimeException(){ |
| 12 | + |
| 13 | + @RequestMapping(value = "500Error", method = RequestMethod.GET) |
| 14 | + public void throwRuntimeException() { |
15 | 15 | throw new NullPointerException("Throwing a null pointer exception"); |
16 | 16 | } |
17 | | - |
18 | 17 |
|
19 | | - @RequestMapping(value = "errors" , method = RequestMethod.GET) |
20 | | - public ModelAndView renderErrorPages(HttpServletRequest httpRequest){ |
| 18 | + @RequestMapping(value = "errors", method = RequestMethod.GET) |
| 19 | + public ModelAndView renderErrorPages(HttpServletRequest httpRequest) { |
21 | 20 | ModelAndView errorPage = new ModelAndView("errorPage"); |
22 | 21 | String errorMsg = ""; |
23 | 22 | int httpErrorCode = getErrorCode(httpRequest); |
24 | | - |
25 | | - switch( httpErrorCode ){ |
26 | | - case 400:{ |
27 | | - errorMsg = "Http Error Code : 400 . Bad Request"; |
28 | | - break; |
29 | | - } |
30 | | - case 401:{ |
31 | | - errorMsg = "Http Error Code : 401. Unauthorized"; |
32 | | - break; |
33 | | - } |
34 | | - case 404:{ |
35 | | - errorMsg = "Http Error Code : 404. Resource not found"; |
36 | | - break; |
37 | | - } |
38 | | - //Handle other 4xx error codes. |
39 | | - case 500:{ |
40 | | - errorMsg = "Http Error Code : 500. Internal Server Error"; |
41 | | - break; |
42 | | - } |
43 | | - //Handle other 5xx error codes. |
| 23 | + |
| 24 | + switch (httpErrorCode) { |
| 25 | + case 400: { |
| 26 | + errorMsg = "Http Error Code : 400 . Bad Request"; |
| 27 | + break; |
| 28 | + } |
| 29 | + case 401: { |
| 30 | + errorMsg = "Http Error Code : 401. Unauthorized"; |
| 31 | + break; |
| 32 | + } |
| 33 | + case 404: { |
| 34 | + errorMsg = "Http Error Code : 404. Resource not found"; |
| 35 | + break; |
| 36 | + } |
| 37 | + // Handle other 4xx error codes. |
| 38 | + case 500: { |
| 39 | + errorMsg = "Http Error Code : 500. Internal Server Error"; |
| 40 | + break; |
| 41 | + } |
| 42 | + // Handle other 5xx error codes. |
44 | 43 | } |
45 | 44 | errorPage.addObject("errorMsg", errorMsg); |
46 | 45 | return errorPage; |
47 | 46 | } |
48 | | - |
49 | | - private int getErrorCode(HttpServletRequest httpRequest){ |
| 47 | + |
| 48 | + private int getErrorCode(HttpServletRequest httpRequest) { |
50 | 49 | return (Integer) httpRequest.getAttribute("javax.servlet.error.status_code"); |
51 | 50 | } |
52 | 51 | } |
0 commit comments