error.stack


error.stack 属性是描述代码中实例化 Error 的点的字符串。

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 无法确定函数的名称,则只会显示该帧的位置信息。 否则,将显示确定的函数名称,括号中会附加位置信息。

帧仅为 JavaScript 函数生成。 例如,如果执行同步通过名为 cheetahify 的 C++ 插件函数,该函数本身调用 JavaScript 函数,则表示 cheetahify 调用的帧将不会出现在堆栈跟踪中:

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

function makeFaster() {
  // `cheetahify()` 同步地调用 speedy。
  cheetahify(function speedy() {
    throw new Error('oh no!');
  });
}

makeFaster();
// 会抛出:
//   /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

位置信息将是以下之一:

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

访问 error.stack 属性时,延迟生成表示堆栈跟踪的字符串。

堆栈跟踪捕获的帧数以 Error.stackTraceLimit 或当前事件循环刻度上的可用帧数中的较小者为界。

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)

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.

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, if the frame represents a call internal to V8 (as in [].forEach).
  • plain-filename.js:line:column, if the frame represents a call internal to Node.js.
  • /absolute/path/to/file.js:line:column, if the frame represents a call in a user program (using CommonJS module system), or its dependencies.
  • <transport-protocol>:///url/to/module/file.mjs:line:column, if the frame represents a call in a user program (using ES module system), or its dependencies.

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

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.