|
2 | 2 |
|
3 | 3 | import javax.servlet.http.HttpServletRequest; |
4 | 4 |
|
| 5 | +import org.springframework.http.converter.xml.MarshallingHttpMessageConverter; |
5 | 6 | import org.springframework.stereotype.Controller; |
6 | 7 | import org.springframework.web.bind.annotation.RequestMapping; |
7 | 8 | import org.springframework.web.bind.annotation.RequestMethod; |
8 | 9 | import org.springframework.web.servlet.ModelAndView; |
9 | 10 |
|
10 | 11 | @Controller |
11 | 12 | public class ErrorController { |
| 13 | + |
| 14 | + @RequestMapping(value = "500Error", method = RequestMethod.GET) |
| 15 | + public void throwRuntimeException() { |
| 16 | + throw new NullPointerException("Throwing a null pointer exception"); |
| 17 | + } |
12 | 18 |
|
13 | | - @RequestMapping(value = "500Error", method = RequestMethod.GET) |
14 | | - public void throwRuntimeException() { |
15 | | - throw new NullPointerException("Throwing a null pointer exception"); |
16 | | - } |
| 19 | + @RequestMapping(value = "errors", method = RequestMethod.GET) |
| 20 | + public ModelAndView renderErrorPage(HttpServletRequest httpRequest) { |
| 21 | + ModelAndView errorPage = new ModelAndView("errorPage"); |
| 22 | + String errorMsg = ""; |
| 23 | + int httpErrorCode = getErrorCode(httpRequest); |
17 | 24 |
|
18 | | - @RequestMapping(value = "errors", method = RequestMethod.GET) |
19 | | - public ModelAndView renderErrorPages(HttpServletRequest httpRequest) { |
20 | | - ModelAndView errorPage = new ModelAndView("errorPage"); |
21 | | - String errorMsg = ""; |
22 | | - int httpErrorCode = getErrorCode(httpRequest); |
| 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. |
| 44 | + } |
| 45 | + errorPage.addObject("errorMsg", errorMsg); |
| 46 | + return errorPage; |
| 47 | + } |
23 | 48 |
|
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. |
43 | | - } |
44 | | - errorPage.addObject("errorMsg", errorMsg); |
45 | | - return errorPage; |
46 | | - } |
47 | | - |
48 | | - private int getErrorCode(HttpServletRequest httpRequest) { |
49 | | - return (Integer) httpRequest.getAttribute("javax.servlet.error.status_code"); |
50 | | - } |
| 49 | + private int getErrorCode(HttpServletRequest httpRequest) { |
| 50 | + return (Integer) httpRequest |
| 51 | + .getAttribute("javax.servlet.error.status_code"); |
| 52 | + } |
51 | 53 | } |
0 commit comments