我可以使用原始堆栈和堆栈帧重新创建错误对象吗?

Can I re-create an error object with the original stack and stack frames?

我熟悉如何在 JavaScript 中创建一个 custom Error object 像这样。

class CustomError extends Error {
  constructor(args) {
    super(...args);
    Error.captureStackTrace(this, CustomError);
  }
}

但是鉴于 exception/error 已经在其他地方抛出,我想创建一个新的错误对象,它是原始对象的 clone/copy,包括堆栈。

我的上下文是我正在使用日志报告程序,例如Winston, to capture events and I would like to post error messages to Sentry. Sentry provides a way to capture exceptions 像这样 -

try {
    aFunctionThatMightFail();
} catch (err) {
    Sentry.captureException(err);
}

但问题在于,Sentry 假设错误被捕获的地方就是错误被抛出的地方。

Sentry 的一个好处是它可以报告应用程序中发生错误的行号,但是因为我正在聚合日志,原始错误的堆栈帧已经丢失。我可以保存额外的元数据,我可以将其发送给哨兵,但它仍然突出显示 Sentry.captureException 作为错误来源的行和调用 Winston 的堆栈帧。

Sentry SDK assemble 是来自您传递给 captureExceptionError 对象的 JSON 负载。我认为您只想直接 assemble 该有效负载并使用 captureEvent 发送它。有关详细信息,请参阅 https://docs.sentry.io/development/sdk-dev/attributes/