如何更改Sentry中使用的JavaScript错误对象的名称或消息?

发布于 2025-01-26 12:21:47 字数 331 浏览 5 评论 0原文

我有一个用代码捕获的JavaScript错误对象。它具有我想登录后端的名称,消息,堆栈等。我正在使用哨兵。但是在登录之前,我想更改错误的名称或消息。

最好的方法是什么? 我尝试创建一个新的错误,并将原始错误添加为原因,但这与Sentry不起作用。它只是将传递的错误记录为新错误的原因。

new Error('Additional error message', { cause: originalError });

我需要错误的其余属性保持不变,只需更改名称或消息即可。

I have a JavaScript error object that I have caught in code. It has a name, message, stack etc that I want to log at the backend. I am using sentry for that. But before logging I want to change the name or the message of the error.

What will be the best way to do it?
I tried creating a new error and adding the original error as cause, but that did not work with sentry. It just logs the error passed as the cause of the new error.

new Error('Additional error message', { cause: originalError });

I need the rest of the properties of the error to remain the same, just need to change the name or message.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

书间行客 2025-02-02 12:21:47

我对此有一些可读性的错误:

当您捕获异常时,将TransActionName添加到scope

您还可以在beforesend方法中增强事件

Sentry.captureException(error, (scope) => {
        ...
        scope.setTransactionName(`my custom title for error`);
        return scope;
    });

“

I've made errors a bit readable with this:

when you capture exception, add transactionName to scope.

you can also enhance event in beforeSend method

Sentry.captureException(error, (scope) => {
        ...
        scope.setTransactionName(`my custom title for error`);
        return scope;
    });

Sentry error example:

在巴黎塔顶看东京樱花 2025-02-02 12:21:47

您可以做的一件超级有用的事情实际上是创建自己的自定义错误类型。这可以通过简单地使用扩展错误构造函数的类来完成,例如:

class MyError extends Error {
  constructor(message) {
    super();
    this.name = "MyError";
    this.message = message;
  }
}
try {
  throw new MyError('this is my error')
} catch (err) {
  console.log(err instanceof Error);
  console.log(err.message);
  console.log(err.name);
  console.log(err.stack);
}

class ExtendedError extends Error {
  constructor(message, { cause }) {
    super();
    this.name = "ExtendedError";
    this.message = message;
    // set the cause to maintain linkage to the original error
    this.cause = cause;
  }
}
try {
  throw new Error('Something bad happened!');
} catch (err) {
  let extendedError = new ExtendedError('Additional details', { cause: err });
  console.log(extendedError instanceof Error);
  console.log(extendedError.message);
  console.log(extendedError.name);
  console.log(extendedError.cause.stack);
  console.log(extendedError.stack);
}

A super helpful thing you can do to accomplish this is actually create your own custom error types. This can be done by simply using a class that extends the Error constructor, like so:

class MyError extends Error {
  constructor(message) {
    super();
    this.name = "MyError";
    this.message = message;
  }
}
try {
  throw new MyError('this is my error')
} catch (err) {
  console.log(err instanceof Error);
  console.log(err.message);
  console.log(err.name);
  console.log(err.stack);
}

class ExtendedError extends Error {
  constructor(message, { cause }) {
    super();
    this.name = "ExtendedError";
    this.message = message;
    // set the cause to maintain linkage to the original error
    this.cause = cause;
  }
}
try {
  throw new Error('Something bad happened!');
} catch (err) {
  let extendedError = new ExtendedError('Additional details', { cause: err });
  console.log(extendedError instanceof Error);
  console.log(extendedError.message);
  console.log(extendedError.name);
  console.log(extendedError.cause.stack);
  console.log(extendedError.stack);
}

メ斷腸人バ 2025-02-02 12:21:47

除了@anastasia plaskevich的答案外,您似乎还可以更改这样的错误类型:

Sentry.captureException(error, (scope) => {
scope.addEventProcessor((event) => {
if (event.exception?.values) {
event.exception.values[0] = {
...event.exception.values[0],
type: '

In addition to @Anastasia Plaskevich's answer, It appears that you can also change the Error type like this:

Sentry.captureException(error, (scope) => {
    scope.addEventProcessor((event) => {
        if (event.exception?.values) {
            event.exception.values[0] = {
                ...event.exception.values[0],
                type: '????‍♂️ My Bad Error',
            };
        }
        return event;
    });
  return scope;
});

Then you'll see something like this:

custom sentry error type

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文