views:

243

answers:

1

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 swallowed by some exception handling code and the whole process then silently terminates. No exceptions are ever thrown to the browser and it is a bit tedious to debug (for understandable reasons). Is there any straightforward way to prevent this from happening?

A: 

I think you can include an onException callback handler with the request options:

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

To do it on a global level, the callback handler can be registered with the Ajax.Responders object:

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

The first argument will be the Ajax.Request object that triggered the callback.

P.S./FYI: Your exception is being concealed by this code in the Prototype source:

dispatchException: function(exception) {
  (this.options.onException || Prototype.emptyFunction)(this, exception);
  Ajax.Responders.dispatch('onException', this, exception);
}
Tomalak
I knew of the onException parameter, but the Ajax.Responders.register-function was new to me. If I want all errors to spill to the browser during debugging this code would answer my needs: Ajax.Responders.register({ onException: function(request, ex) { if (debug) { throw ex; } } }); Then i just need to set the debug flag to true to have all errors happening within code called from the Ajax-object thrown to the browser. Thank you!
Aleksander Krzywinski
Glad to help. If this answer solves your problem, you may mark it as "accepted". :)
Tomalak
Sorry. I', new here, so I didn't know the routine :)
Aleksander Krzywinski