The pattern we have used is to have a BaseController
that all our Controllers extend using the following format to have specific errors mapped to specific HTTP status and a catch for more the most generic in Exception:
@Controller
public class BaseController {
@ExceptionHandler (Exception.class)
@ResponseStatus (HttpStatus.INTERNAL_SERVER_ERROR)
public ModelAndView handleAllExceptions(Exception ex) {
return new JsonError(ex.getMessage()).asModelAndView();
}
@ExceptionHandler (InvalidArticleQueryRangeException.class)
@ResponseStatus (HttpStatus.NOT_FOUND)
public ModelAndView handleAllExceptions(InvalidArticleQueryRangeException ex) {
return new JsonError(ex.getMessage()).asModelAndView();
}
}