Reputation: 11
I'm having trouble redirecting any exception to an error page. When the error occurs, the method is called, but there is no redirection of the page, continuing in the same one that performs the action. The method code looks like this:
@ControllerAdvice
public class GlobalExceptionHandler extends RuntimeException {
private Logger logger = Logger.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception{
logger.info("Tratando erro de exception.");
logger.error("ERRO: " + e);
ModelAndView mav = new ModelAndView();
mav.setViewName("vixe");
return mav;
}
}
The screenshot shows that the exception occurred at runtime, but nothing changes on the page: Runtime error
Upvotes: 0
Views: 312
Reputation: 11
I finally understood what was going on. The fact that javascript makes the request and triggers the error in order to return the message made calling the view in the treatment method unnecessary. What I did was treatment with ajax making the page call in case of request error.
$.ajaxSetup({
error: function (x) {
if (x.status == 500) {
window.location.replace("vixe")
}
}
});
So, if I call at page load or at the end of requests, the error checking is done and the error screen is triggered.
Upvotes: 1