我正在使用淘汰赛编写应用程序,我希望能够捕获在我的代码中运行的淘汰赛中的任何错误,例如订阅。如何在淘汰订阅中处理错误
目前,我有一个淘汰赛订阅:
var myObservable = ko.observable();
myObservable.subscribe(function (val) {
// Code here is error prone
});
我希望能够使用上述模式在我的应用程序,但能赶上在订阅回调抛出的任何错误。
我目前的解决方案是用一个错误处理包裹ko.subbscribable.fn.subscribe功能,像这样:
var _subscribe = ko.subscribable.fn.subscribe;
ko.subscribable.fn.subscribe = function (callback) {
if (arguments.length != 1) return _subscribe.apply(this, arguments);
else return _subscribe.apply(this, [function() {
try
{
callback.apply(this, arguments);
}
catch (err) {
// handleError is a function in my code which will handle the error for me
handleError(err);
}
}]);
};
我的问题是是否有任何隐藏的一面用这种方法影响,或者是有我错过了一个更好的方法。
对我来说似乎很好。在http://codereview.stackexchange.com/ –