javascript – 原型回调函数吞噬异常

使用Prototype版本1.6.0.2.

我有一个常见的问题,当异常被抛入回调函数时被吞下,通常当我试图处理对Ajax.Request调用的响应时.这是一个简单的例子:

HTML标记

使用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.");
};

好的,所以问题是,如果你在Firebug的Firefox中运行这个代码,那么对于有问题的行也不会输出异常 – 它被吞没了.咕嘟咕嘟.
我知道捕获这些的唯一方法(比如说我正在调试)是在try / catch中包装回调函数内容.例如:

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);
    }
};

还有其他人遇到过这个问题吗?那里有任何解决方法吗?

提前致谢!

最佳答案
截至今天,这是已知的行为:

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

这里有一个增强处理这些吞下的异常的票证:

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

一个解决方法建议添加以下代码(感谢Glenn Maynard!):

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

希望在实施更持久的解决方案之前帮助其他人解决同样的问题.

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...