原型回调函数吞掉异常
使用原型版本 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);
}
};
还有其他人遇到过这个问题吗?有什么解决方法吗?
提前致谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
截至今天,这是已知的行为:
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!):
希望能帮助其他人解决相同的问题,直到实施更永久的解决方案。
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!):
Hope that helps others with the same issue until a more permanent solution is implemented.