error.stack
- 类型:<string>
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.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.
error.stack 是一个用于隐藏内部属性的 getter/setter,该属性仅存在于内置 Error 对象上(对于这些对象,Error.isError 返回 true)。如果 error 不是内置错误对象,则 error.stack getter 总是返回 undefined,而 setter 将不执行任何操作。如果带有非内置错误对象的 this 值手动调用访问器(例如 <Proxy>),则可能发生这种情况。