如何累积JavaScript错误? [英] How to roll up a JavaScript error?

查看:66
本文介绍了如何累积JavaScript错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我有意抛出一个错误,但是在Chrome(仅用于测试目的)中,它并没有引起人们的注意。如何将错误汇总到父母的范围内?

In the following code, I intentionally throw an error, but in Chrome (used simply for testing purposes) it does not roll up to the catch. How can I roll up the error into the parent's scope?

try {
  setTimeout(function() {
    console.log("Throwing Error...");
    throw({message:"Ouch!"});
  }, 500);
} catch(e) {
  console.log(e.message);
}

Chrome回复:

Uncaught #<Object>
  (anonymous function)

以下是我正在使用的完整示例;当我需要鲍勃时(故意)超时。我想捕获requirejs错误,以便可以使用应用程序的错误系统(它更强大)来通知学习者。

Here is the full example I'm working with; when I require "bob" it (intentionally) times out. I want to catch the requirejs error so I could use my application's error system, which is more robust, to notify the learner.

(function() {
try {
  var scriptVersion = "1.0.0.1"
  window.onload = function() {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "//content.com/pkg/" + scriptVersion + "/require-jquery.js";
    script.async = false;
    script.done = false;
    // OnReadyStateChange for older IE browsers
    script.onload = script.onreadystatechange = function() {
      if(!(this.done) && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
        this.done = true;
        require.config({
          baseUrl: "//content.com/pkg/" + scriptVersion
        });
        require(["bob"]);
      }
    }
    document.getElementsByTagName("head")[0].appendChild(script);
  }
} catch(e) {
  console.log(e);
}
})();


推荐答案

有关如何解决实际问题的信息,请参见下面的编辑

See the edit below for how to solve the actual problem with requireJS.

问题是 setTimeout()函数在父级作用域中运行,并且可以正确完成。它计划(与系统一起)将来的回调事件,但是当将来发生该回调时,父级的执行范围已经完成,并且像新系统事件(例如,单击)一样从顶层系统启动了回调。事件处理程序。)

The problem is that the setTimeout() function runs in the parent's scope and completes without error. It schedules (with the system) a future callback event, but when that callback occurs in the future, the parent's scope of execution has finished and the callback is initiated from the system at the top level much like a new system event (e.g. a click event handler).

父闭包仍然存在,因为 setTimeout()中的匿名函数仍然可以引用这些变量将完成父作用域的实际执行,从而完成try / catch的作用域。

While the parent closure still exists because the anonymous function inside the setTimeout() can still reference those variables, the actual execution of the parent scope is done, thus the scope of the try/catch is done.

的执行上下文setTimeout()匿名函数是顶级的(由系统启动),因此没有父上下文可以将try / catch放入。可以将try / catch放入匿名函数中,但是从那里抛出的内容只会返回到称为 setTimeout()回调的系统。

The execution context of the setTimeout() anonymous function is top level (initiated by the system) so there is no parent context that you can put a try/catch in. You can put a try/catch within the anonymous function, but throwing from there will just go back to the system which is what called the setTimeout() callback.

您自己的代码捕获 setTimeout()调用内发生的任何异常返回,则需要在回调中放入try / catch。

To have your own code catch any exceptions that occur inside the setTimeout() callback, you will need to put a try/catch inside the callback.

  setTimeout(function() {
    try {
        console.log("Throwing Error...");
        throw({message:"Ouch!"});
    } catch(e) {
        console.log(e.message);
    }
  }, 500);

如果您解释了真正要解决的问题是什么(而不是此人工测试)情况),我们也许可以提供一些有用的选项。

If you explained what the real problem is that you're trying to solve (rather than this manufactured test case), we may be able to offer some useful options.

编辑已经显示出您真正要解决的问题。 require.js库通过调用 onError 方法来启动每个错误。 onError 方法的默认实现是引发异常的原因。您可以分配自己的 onError 处理程序,并在回调而不是异常中处理错误。

Edit now that you've shown what problem you're really trying to solve. The require.js library initiates every error by calling the onError method. The default implementation of the onError method is what throws the exception. You can assign your own onError handler and handle the errors in a callback rather than with exceptions. This sounds like the right way to go.

来自requirejs来源:

From the requirejs source:

/**
 * Any errors that require explicitly generates will be passed to this
 * function. Intercept/override it if you want custom error handling.
 * @param {Error} err the error object.
 */
req.onError = function (err) {
    throw err;
};

这篇关于如何累积JavaScript错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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