最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can i change the name or message of a JavaScript Error object used in Sentry? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited May 9, 2022 at 6:29 ghostCoder asked May 5, 2022 at 20:59 ghostCoderghostCoder 7,6559 gold badges53 silver badges74 bronze badges 1
  • What do you mean by "before logging"? Can you show the plete code, as well as your Sentry JS configuration? Did you re-throw the new error? – Bergi Commented May 5, 2022 at 22:58
Add a ment  | 

3 Answers 3

Reset to default 10

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;
    });

A super helpful thing you can do to acplish 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);
}

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: '
发布评论

评论列表(0)

  1. 暂无评论