原型回调函数吞咽异常 [英] Prototype callback functions swallowing exceptions
问题描述
使用原型版本1.6.0.2。
Using Prototype version 1.6.0.2.
我有例外情形,当他们抛出一个回调函数,正在吞噬一个共同的问题,通常,当我试图处理响应的Ajax.Request
通话。下面是一个简单的例子:
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标记:
<input type="button" id="myButton" value="Press Me" />
JavaScript的:
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.");
};
好了,问题是,如果你运行此code在Firefox与萤火虫也不例外,输出的违规行 - 它吞噬。咕嘟咕嘟。 我知道,只有这样,才能捕捉这些(比方说,如果我调试)是包装在一个try / catch回调函数的内容。例如:
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?
在此先感谢!
推荐答案
从今天起,这就是所谓的行为:
As of today, this is known behaviour:
<一个href="http://groups.google.com/group/prototype-scriptaculous/browse_thread/thread/e71c7a6bfb656380/7d1c8a23edc07f03?lnk=gst&q=exception+swallowed#" rel="nofollow">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:
<一个href="https://prototype.lighthouseapp.com/projects/8886/tickets/634-no-exception-on-error-in-oncreate-method-of-ajaxrequest" rel="nofollow">https://prototype.lighthouseapp.com/projects/8886/tickets/634-no-exception-on-error-in-oncreate-method-of-ajaxrequest
一个解决方法建议是添加以下code(感谢格伦·梅纳德!):
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.
这篇关于原型回调函数吞咽异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!