原型回调函数吞掉异常

发布于 2024-08-28 07:42:22 字数 1615 浏览 4 评论 0 原文

使用原型版本 1.6.0.2。

我有一个常见的问题,异常在回调函数中抛出时被吞没,通常是当我尝试处理对 Ajax.Request 调用的响应时。这是一个简单的示例:

HTML 标记:

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

好的,所以问题是,如果您在 Firefox 中使用 Firebug 运行此代码,则违规行不会输出任何异常 - 它会被吞掉。咕噜咕噜。 我知道捕获这些的唯一方法(假设我正在调试)是将回调函数的内容包装在 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);
    }
};

还有其他人遇到过这个问题吗?有什么解决方法吗?

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月棠 2024-09-04 07:42:22

截至今天,这是已知的行为:

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

希望能帮助其他人解决相同的问题,直到实施更永久的解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文