大网数据广告活动
Web 标准与前端开发

Error.captureStackTrace()

· 52CE 我爱测

The **`Error.captureStackTrace()`** static method installs stack trace information on a provided object as the `stack` property.

The Error.captureStackTrace() static method installs stack trace information on a provided object as the stack property.

Syntax

Error.captureStackTrace(object)
Error.captureStackTrace(object, constructor)

Parameters

  • object
    • : The object on which to add the stack property.
  • constructor
    • : A function, typically the constructor where the object was created. When collecting the stack trace, all frames above the topmost call to this function, including that call, are left out of the stack trace.

Return value

None (undefined).

The object is modified in-place with an extra own property called stack defined, whose string value follows the same format as Error.prototype.stack. This property is non-enumerable and configurable. In V8, it is a getter-setter pair. In SpiderMonkey and JavaScriptCore, it is a data property that is writable.

Examples

Using Error.captureStackTrace()

The getStack() utility function returns the current stack trace at the point it is called, removing itself from the stack. This serves the same debugging purpose as console.trace(), but allows you to output the string elsewhere. Note that it does not construct an Error instance for this purpose, but installs stack on a plain object, which would be more efficient for our purposes. Normally, you would call Error.captureStackTrace on objects intended to be thrown as errors, as shown in the next example.

function getStack() {
  const obj = {};
  if ("captureStackTrace" in Error) {
    // Avoid getStack itself in the stack trace
    Error.captureStackTrace(obj, getStack);
  }
  return obj.stack;
}

function foo() {
  console.log(getStack());
}

foo();
// Error
//     at foo (<anonymous>:8:15)
//     at <anonymous>:11:1

Installing stack trace on a custom error object

The main use case for Error.captureStackTrace() is to install a stack trace on a custom error object. Typically, you define custom errors by extending the Error class, which automatically makes the stack property available via inheritance. However, the problem with the default stack trace is that it includes the constructor call itself, which leaks implementation details. You can avoid this by using Error.captureStackTrace(), which allows the stack trace to be installed even for custom errors that do not inherit from Error.

class MyError extends Error {
  constructor(message, options) {
    super(message, options);
    if ("captureStackTrace" in Error) {
      // Avoid MyError itself in the stack trace
      Error.captureStackTrace(this, MyError);
    }
  }
}

const myError = new MyError("Something went wrong");
console.log(myError.stack);
// Error: Something went wrong
//     at <anonymous>:8:17

Note that even if you don't call Error.captureStackTrace() here, some engines are still smart enough to avoid MyError in the stack trace if the constructor inherits from Error. Calling Error.captureStackTrace() is more important for custom errors that, for some reason, do not inherit from Error.

class MyError {
  constructor(message) {
    this.message = message;
    if ("captureStackTrace" in Error) {
      // Avoid MyError itself in the stack trace
      Error.captureStackTrace(this, MyError);
    }
  }
}

const myError = new MyError("Something went wrong");
console.log(myError.stack);
// Error: Something went wrong
//     at <anonymous>:8:17

Specifications

Browser compatibility

See also

  • Error.prototype.stack
  • Error.stackTraceLimit
  • Stack trace API in the V8 docs

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。