What's the most convenient way to stop the Ajax object in the prototype from swallowing exceptions?

Consider this code:

new Ajax.Request('?service=example', {
    parameters : {tags : 'exceptions'},
    onSuccess : this.dataReceived.bind(this)
});

      

If an exception occurs anywhere in the dataReceived function when it is called by the Ajax object, it is consumed by some exception handling code and the whole process then silently exits. No exceptions are thrown at the browser and is a bit tedious to debug (for obvious reasons). Is there an easy way to prevent this?

+1


a source to share


1 answer


I think you can include a callback handler onException

with request parameters:

new Ajax.Request('?service=example', {
  parameters : {tags : 'exceptions'},
  onSuccess : this.dataReceived.bind(this),
  onException : function(request, ex) { // react somehow }
});

      

To do this globally, a callback handler can be registered with Ajax.Responders

object
:

Ajax.Responders.register({
  onException: function(request, ex) {
    // react somehow
  }
});

      

The first argument will be the object Ajax.Request

that invoked the callback.



PS / FYI: Your exception is hidden by this code in Prototype Source :

dispatchException: function(exception) {
  (this.options.onException || Prototype.emptyFunction)(this, exception);
  Ajax.Responders.dispatch('onException', this, exception);
}

      

Here's how to properly execute the onException callback.

Ajax.Responders.register({ 
        onException: function(request, exception) { 
                (function() { throw exception; }).defer(); 
        } 
});

      

As described here: fooobar.com/questions/1043544 / ...

+3


a source







All Articles