Results 1 to 5 of 5

Thread: Handling Exception and Http Error codes

  1. #1
    Join Date
    Mar 2011
    Posts
    27

    Default Handling Exception and Http Error codes

    Hi,
    This question is addressed to the forum in general but to Marten Deinum specifically.
    I am trying to find a clean way to handle all Exceptions as well as http error codes (such as 400, 404 etc...) and map them to the same custom view. I am using Spring MVC 3.0.5
    Any help on this will be very much appreciated.

    Emmanuel

  2. #2
    Join Date
    Mar 2011
    Posts
    27

    Default

    By the way here is what I have done so far:
    [B]web.xml
    Code:
    <web-app id="WebApp_ID" version="2.5"
    	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    	<display-name>myApp</display-name>
    	<error-page>
    		<error-code>400</error-code>
    		<location>/errorPage400.ftl</location>
    	</error-page>
    	<error-page>
    		<error-code>401</error-code>
    		<location>/errorPage401.ftl</location>
    	</error-page>
    	<error-page>
    		<error-code>404</error-code>
    		<location>/errorPage404.ftl</location>
    	</error-page>
    	<error-page>
    		<error-code>500</error-code>
    		<location>/errorPage500.ftl</location>
    	</error-page>
    	<error-page>
            <exception-type>java.lang.Throwable</exception-type>
            <location>/errorPage.ftl</location>
        </error-page>
        .
        .
        .
    My Controller:

    Code:
    @Controller
    public class ErrorCodeController extends BaseController {
    
    	@ExceptionHandler(ApplicationException.class)
    	@RequestMapping(value="errorPage400", method=RequestMethod.GET)
    	@ResponseStatus(value = HttpStatus.BAD_REQUEST)
    	public String handleBadRequest(ApplicationException ex,HttpServletResponse response, ModelMap map) { 
    		map.addAttribute("http-error-code", HttpStatus.BAD_REQUEST);
    		return processErrorCodes(ex,response,map);
    	}
    	
    	
    	@ExceptionHandler(ApplicationException.class)
    	@RequestMapping(value="errorPage401", method=RequestMethod.GET)
    	@ResponseStatus(value=HttpStatus.UNAUTHORIZED,reason="Unauthorized Request")
    	public String handleUnauthorizedRequest(ApplicationException ex,HttpServletResponse response, ModelMap map) { 
    		map.addAttribute("http-error-code", HttpStatus.UNAUTHORIZED);
    		return processErrorCodes(ex,response,map);
    	}
    	
    	
    	@ExceptionHandler(ApplicationException.class)
    	@RequestMapping(value="errorPage404", method=RequestMethod.GET)
    	@ResponseStatus(HttpStatus.NOT_FOUND)
    	public String handleNotFoundRequest(ApplicationException ex,HttpServletResponse response, ModelMap map) { 
    		map.addAttribute("http-error-code", HttpStatus.NOT_FOUND);
    		return processErrorCodes(ex,response,map);
    	}
    
    	
    	@ExceptionHandler(ApplicationException.class)
    	@RequestMapping(value="errorPage500", method=RequestMethod.GET)
    	@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR,reason="Internal Server Error")
    	public String handleInternalServerError(ApplicationException ex,HttpServletResponse response, ModelMap map) { 
    		map.addAttribute("http-error-code", HttpStatus.INTERNAL_SERVER_ERROR);
    		return processErrorCodes(ex,response,map);
    	}
    	
    	@ExceptionHandler(ApplicationException.class)
    	public void handleApplicationExceptions(Throwable exception, HttpServletResponse response) {
    		
    	}
    	
    	private String processErrorCodes(ApplicationException ex,HttpServletResponse response, ModelMap map){
    		map.addAttribute("class", ClassUtils.getShortName(ex.getClass()));
    		map.addAttribute("message", ex.getMessage());
    		map.addAttribute("errorMessage", ex.getErrorMessage());
    		map.addAttribute("errorCode", ex.getErrorCode());
    		map.addAttribute("timestamp", ex.getCurrentDate());
    		return "errorPage";
    	}
    }
    My base Controller:

    Code:
    @Controller
    @RequestMapping("/")
    public class BaseController {
    
    	// Remember to add any exception that you suspect can be thrown in this web application.
    	@ExceptionHandler({ApplicationException.class,NullPointerException.class})
    	public ModelAndView handleException(Throwable exception,HttpServletRequest req) {
    
    		ModelMap model = new ModelMap();
    		ApplicationException ex = new ApplicationException();
    		String timeStamp = ex.getCurrentDate().toString();
    		//String temp = ClassUtils.getShortName(ex.getClass());
    		//model.addAttribute("class", ClassUtils.getShortName(ex.getClass()));
    		model.addAttribute("timeStamp", timeStamp);
    		return new ModelAndView("errorPage", model);
    	} 
    }
    So again I want to know if this is the best way to map any exception and http error code to a custom view.
    Thanks,

    Emmanuel
    Last edited by emmanuel; Dec 27th, 2011 at 11:16 AM.

  3. #3
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    You can configure a SimpleExceptionResolver (instead of the above controllers + web.xml error pages) setting its exceptionMappings, and defaultStatusCodes properties. See the Javadoc on both.

  4. #4
    Join Date
    Mar 2011
    Posts
    27

    Default

    Thanks for the reply and suggestion.

    Emmanuel

  5. #5
    Join Date
    Feb 2012
    Location
    Indonesia
    Posts
    1

    Default

    Hi there,

    i've used spring mvc with xml base configuration and register the error page in web.xml . Now i'm trying to learn mvc with no xml configuration (servlet-3) and i can not find the way to register the errorpage. could somebody tell me how to register a page for handling an errorcode like 404(resource not found on sever)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •