如何捕获传递给jQuery的回调中抛出的异常? [英] How to catch exceptions thrown in callbacks passed to jQuery?

查看:433
本文介绍了如何捕获传递给jQuery的回调中抛出的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获从传递给jQuery的回调中抛出的异常(对点击或jqXHR方法如

I'd like to catch exceptions thrown from callbacks passed to jQuery (either to event handlers like click, or to jqXHR methods such as then or always).

我已经确定了两个选项:

I've identified two options:


  • window.onerror 处理程序 - 这只是一个部分解决方案,因为它不支持Android这是我的目标平台之一

  • window.onerror handler - this is only a partial solution because it isn't supported on Android which is one of my target platforms
  • handling exceptions within each individual callback - not DRY at all!

我唯一能想到的是覆盖jQuery方法,但这可能会导致问题,任何时候我升级jQuery。对于AJAX处理程序,我可以使用$ .ajaxSetup(根据 jQuery AJAX回调中抛出的异常?)的答案?,但是我不确定是否会让我抓住一切。

The only other thing I can think of is overriding jQuery methods but that can result in problems any time I upgrade jQuery. For AJAX handlers, I could possibly use $.ajaxSetup (per the answer to Exceptions thrown in jQuery AJAX callbacks swallowed?) but I'm not sure that will allow me to catch everything.

还有其他选择吗?

推荐答案

您可以这样封装每个回调:

You can wrap each callback like this:

function cbWrapper(fn) {
    return function() {
        try {
            return(fn.apply(this, arguments));
        } catch(e) {
            // handle all your exceptions here
        }
    };
}

因此,当你将一个回调传递给Ajax调用时,传递实际的回调,你传递 cbWrapper(callback)

So, when you go to pass a callback to an Ajax call, instead of passing the actual callback, you pass cbWrapper(callback).

$.get(url, cbWrapper(myRealCallback));

或者内联匿名版本如下:

Or the inline anonymous version would be like this:

$.get(url, cbWrapper(function() {
    // actual callback code here
}));

更高级的方法是覆盖 $。get )用你自己的实现,自动包装回调,但是变得棘手,因为jQuery是如此灵活的什么参数实际传递。因为这一点,并且因为你必须重写一个特定的参数来使这个工作,你将不得不复制所有的参数检测代码,确定哪些参数存在,哪些对应于哪些实际的函数参数。该代码有点凌乱。它是可行的,可能不会打破,因为如果jQuery打破了,然后现有的jQuery代码会破,但它不是很干净。

A more advanced way of doing it would be to override $.get() with your own implementation that wraps the callback automatically, but that gets tricky because jQuery is so flexible about what arguments are actually passed. Because of that and because you have to override one specific argument to make this work, you would have to duplicate all of their argument detection code that figures out which arguments are present and which correspond to which actual function parameters. That code is a bit messy. It's doable and probably won't break because if jQuery broke it, then existing jQuery code would break, but it isn't very clean.

如果它是所有自己的代码,你可以只是使你自己的版本的 $。get()这是不灵活的参数位置和切换所有的代码使用它而不是实际的 $。get()

If it's all your own code, you could just make your own version of $.get() that isn't so flexible with argument positions and switch all of your code to use it instead of the actual $.get():

jQuery.fn.myGet = function(url, fn) {
    return(jQuery.get(url, cbWrapper(fn)));
}

这篇关于如何捕获传递给jQuery的回调中抛出的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆