Tracking Errors/Exceptions during ajax calls in a view

18 / Mar / 2010 by Imran Mir 2 comments

If you want to keep track of the errors and exceptions on the view, after making an ajax call, then this might be something of your interest :

jQuery().ready(function() {
  jQuery.ajaxSetup({
    error: function(x, e) {
      if (x.status == 0) {
        alert('You are offline!!\n Please Check Your Network.');
      } else if (x.status == 404) {
        alert('Requested URL not found.');
      } else if (x.status == 500) {
        alert('Internel Server Error.');
      } else if (e == 'parsererror') {
        alert('Error.\nParsing JSON Request failed.');
      } else if (e == 'timeout') {
        alert('Request Time out.');
      }
      else if (x.status == '401') {
          alert('My Own Exception');
        }   
    }
  });
});

And in your action, you can use the following code to send a custom exception code along with a custome message:

response.sendError(440,"custom message")

In my project I need the errors and exceptions to be shown on a page which is different from the one from which the ajax request originated. So I added the following statement in the else part :

else {
         window.location = "${createLink(controller: 'apgException', action: 'somethingWrong')}" + "? status=" + x.status +"&message=" + x.statusText
  }

Hope you find it useful.
Imran Mir
imran@intelligrape.com

FOUND THIS USEFUL? SHARE IT

comments (2)

  1. Tiago Faustino

    I use version 1.3.4 of Grails, which still uses Prototype for most of the ajax requests. You have such an example for Prototype?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *