error.stack


error.stack 属性是一个字符串,用于描述 Error 被实例化时代码中的位置。

【The error.stack property is a string describing the point in the code at which the Error was instantiated.】

Error: Things keep happening!
   at /home/gbusey/file.js:525:2
   at Frobnicator.refrobulate (/home/gbusey/business-logic.js:424:21)
   at Actor.<anonymous> (/home/gbusey/actors.js:400:8)
   at increaseSynergy (/home/gbusey/actors.js:701:6) 

第一行的格式为 <error class name>: <error message>,紧接着是一系列堆栈帧(每行以“at ”开头)。每个堆栈帧描述了代码中导致错误生成的调用位置。V8 尝试为每个函数显示名称(通过变量名、函数名或对象方法名),但有时可能找不到合适的名称。如果 V8 无法确定函数名称,该堆栈帧只会显示位置信息。否则,将显示确定的函数名称,并在括号中附加位置信息。

【The first line is formatted as <error class name>: <error message>, and is followed by a series of stack frames (each line beginning with "at "). Each frame describes a call site within the code that lead to the error being generated. V8 attempts to display a name for each function (by variable name, function name, or object method name), but occasionally it will not be able to find a suitable name. If V8 cannot determine a name for the function, only location information will be displayed for that frame. Otherwise, the determined function name will be displayed with location information appended in parentheses.】

只有 JavaScript 函数才会生成帧。例如,如果执行同步地通过一个名为 cheetahify 的 C++ 插件函数,而该函数本身又调用了一个 JavaScript 函数,那么表示 cheetahify 调用的帧将不会出现在堆栈跟踪中:

【Frames are only generated for JavaScript functions. If, for example, execution synchronously passes through a C++ addon function called cheetahify which itself calls a JavaScript function, the frame representing the cheetahify call will not be present in the stack traces:】

const cheetahify = require('./native-binding.node');

function makeFaster() {
  // `cheetahify()` *synchronously* calls speedy.
  cheetahify(function speedy() {
    throw new Error('oh no!');
  });
}

makeFaster();
// will throw:
//   /home/gbusey/file.js:6
//       throw new Error('oh no!');
//           ^
//   Error: oh no!
//       at speedy (/home/gbusey/file.js:6:11)
//       at makeFaster (/home/gbusey/file.js:5:3)
//       at Object.<anonymous> (/home/gbusey/file.js:10:1)
//       at Module._compile (module.js:456:26)
//       at Object.Module._extensions..js (module.js:474:10)
//       at Module.load (module.js:356:32)
//       at Function.Module._load (module.js:312:12)
//       at Function.Module.runMain (module.js:497:10)
//       at startup (node.js:119:16)
//       at node.js:906:3 

位置信息将是以下之一:

【The location information will be one of:】

  • native,如果该堆栈帧表示对 V8 内部的调用(例如 [].forEach)。
  • plain-filename.js:line:column,如果该堆栈帧表示对 Node.js 内部的调用。
  • /absolute/path/to/file.js:line:column,如果该堆栈帧表示用户程序(使用 CommonJS 模块系统)或其依赖中的调用。
  • <transport-protocol>:///url/to/module/file.mjs:line:column,如果该堆栈帧表示用户程序(使用 ES 模块系统)或其依赖中的调用。

表示堆栈跟踪的字符串是在访问 error.stack 属性时懒惰生成的。

【The string representing the stack trace is lazily generated when the error.stack property is accessed.】

堆栈跟踪捕获的帧数受 Error.stackTraceLimit 与当前事件循环滴答上可用帧数较小者的限制。

【The number of frames captured by the stack trace is bounded by the smaller of Error.stackTraceLimit or the number of available frames on the current event loop tick.】