开发者

Prototype callback functions swallowing exceptions

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 (sa开发者_运维知识库y 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!


As of today, this is known behaviour:

http://groups.google.com/group/prototype-scriptaculous/browse_thread/thread/e71c7a6bfb656380/7d1c8a23edc07f03?lnk=gst&q=exception+swallowed#

There is a ticket in for an enhancement to deal with these swallowed exceptions here:

https://prototype.lighthouseapp.com/projects/8886/tickets/634-no-exception-on-error-in-oncreate-method-of-ajaxrequest

One work-around suggested is to add the following code (thanks Glenn Maynard!):

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

Hope that helps others with the same issue until a more permanent solution is implemented.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜