Hi all
Using Prototype version 1.6.0.2.
I have a common problem where exceptions are being swallowed when they are thrown in a callback function, typically when I am trying to handle the response to an Ajax.Request
call. Here is a simple example:
HTML markup:
<input type="button" id="myButton" value="Press Me" />
Javascript:
MYSITE = {};
document.observe("dom:loaded", function () {
// Set up our helper object
MYSITE.pageHelper = new MYSITE.PageHelper();
});
MYSITE.PageHelper = function() {
console.log("PageHelper called.");
$("myButton").observe("click", this.makeCall.bindAsEventListener(this));
};
MYSITE.PageHelper.prototype.makeCall = function() {
console.log("Make call.");
new Ajax.Request(
"remoteCall.cfm",
{
method: 'get',
parameters: "",
onComplete: this.handleCallback.bindAsEventListener(this)
});
};
MYSITE.PageHelper.prototype.handleCallback = function(resp) {
console.log("Start callback processing...");
var x = missingVar + "text"; // This line generates an exception...
console.log("Finished callback processing.");
};
OK, so the issue is that if you run this code in Firefox with Firebug no exception will be output for the offending line - it's swallowed. Gulp. The only way I know to catch these (say if I'm debugging) is to wrap the contents of the callback function in a try/catch. For example:
MYSITE.PageHelper.prototype.handleCallback = function(resp) {
try {
console.log("Start callback processing...");
var x = missingVar + "text"; // This line generates an exception...
console.log("Finished callback processing.");
} catch (e) {
console.log(e);
}
};
Has anyone else ever come across this issue? Any work-arounds out there?
Thanks in advance!