简体   繁体   中英

AJAX General Error Handling

I am currently working on an existing system wherein I have to create a generic error handling for the entire system. The system is using Spring MVC and also AJAX.

Now I have done the error handling for normal requests (not using ajax) but I am having problems with the AJAX part. In the AJAX part, if there is an error/exception, the page does not redirect to the generic error page that I created. It stays on the same screen and nothing happens.

I have done my research regarding this and it seems that for the AJAX part, I need to do it by jQuery redirect.

Since I am working with an existing system, I want to minimize the changes to be made. So my question is: Is it possible to create a generic method that the AJAX part can call automatically without adding additional codes to the 'error:' part of the AJAX call?

Any suggestions would be gladly accepted. :D

You can register an ajaxError event. Here is jQuery's documentation for it.

A code example:

$( document ).ajaxError(function() {
  //do your redirect(s) here
});

and a JSFiddle example

Note that I wanted to simply display the gist of using it, but you can also get which jqXHR object threw the error / re-route depending on which one it was.

Have a look here for generic error handling

Have a look at below working example from my application.

My ExceptionController

 @ExceptionHandler(Exception.class)
    public ModelAndView getExceptionPage(Exception e, HttpServletRequest request) {
        BaseLoggers.exceptionLogger.error("Exception in Controller", e);

        if (isAjax(request)) {

            ModelAndView model = new ModelAndView("forward:/app/webExceptionHandler/ajaxErrorRedirectPage");
            request.setAttribute("errorMessageObject", e.toString());
            return model;

        } else {

            ModelAndView model = new ModelAndView("forward:/app/webExceptionHandler/nonAjaxErrorRedirectPage");
            request.setAttribute("errorMessageObject", e.toString());
            request.setAttribute("errorViewName", "error");
            return model;

        }

    }

// AnotherController where request is forwarded.

    // this mapping is responsible for rendering view for all exceptions in nonAjax calls.
    @RequestMapping(value = "/nonAjaxErrorRedirectPage")
    public String nonAjaxErrorRedirectPage(HttpServletRequest request, Model model) {

        Locale loc = RequestContextUtils.getLocale(request);
        String nonAjaxErrorMsg = messageSource.getMessage("label.error.msg.nonAjaxCalls", null, loc);
        model.addAttribute("errorMessage", nonAjaxErrorMsg);
        String errorViewName = (String) request.getAttribute("errorViewName");
        return errorViewName;
    }

    // this mapping is responsible for sending error message with suitable error code for all exceptions in Ajax calls.
    @RequestMapping(value = "/ajaxErrorRedirectPage")
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public String ajaxErrorRedirectPage(HttpServletRequest request, Model model) {
        Locale loc = RequestContextUtils.getLocale(request);
        String ajaxErrorMsg = messageSource.getMessage("label.error.msg.ajaxCalls", null, loc);
        return ajaxErrorMsg;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM