为什么Sentry在我的React应用中忽略了一些错误?

huangapple go评论72阅读模式
英文:

Why does Sentry ignore some errors in my React app?

问题

我在我的React应用中使用Sentry来捕获错误。我已经意识到Sentry会忽略502和504 HTTP代码的错误。在尝试调试时,我意识到有些React错误也被忽略了。我不知道为什么,我想覆盖这个行为。

这是我的初始化函数:

Sentry.init({
  dsn: "<mydsn>",
  integrations: [
    new Integrations.BrowserTracing(),
    // eslint-disable-next-line @typescript-eslint/no-unsafe-call
    new HttpClientIntegration({
      failedRequestStatusCodes: [502, 504],
    }) as unknown as Integration,
  ],

  tracesSampleRate: 1.0,

  enabled: true,
  release: process.env.REACT_APP_BUILD_TAG,
});

这是我的ErrorBoundary中如何处理错误的方式:

  componentDidCatch(error: unknown, _errorInfo: unknown): void {
    // eslint-disable-next-line no-console
    console.log("TESTING. error : ", error, " , error info : ", _errorInfo);
    Sentry.captureException(error);
  }

这是在此函数内部的日志示例:

Error: Objects are not valid as a React child(...)

在我的看法中,Sentry应该捕获此错误,但它没有捕获。

编辑:
我可能没有在本地正确配置Sentry,因为尝试调试时出现CORS错误,甚至我的captureMessage()也无法到达Sentry。

英文:

I'm using Sentry to catch errors in my React app. I've come to realize that errors with 502 and 504 HTTP codes are ignored by Sentry. While trying to debug it, I've realized that some React errors are ignored too. I don't know why and I'd like to override this behavior.

Here's my init function :

Sentry.init({
  dsn: &quot;&lt;mydsn&gt;&quot;,
  integrations: [
    new Integrations.BrowserTracing(),
    // eslint-disable-next-line @typescript-eslint/no-unsafe-call
    new HttpClientIntegration({
      failedRequestStatusCodes: [502, 504],
    }) as unknown as Integration,
  ],

  tracesSampleRate: 1.0,

  enabled: true,
  release: process.env.REACT_APP_BUILD_TAG,
});

Here's how errors are handled in my ErrorBoundary :

  componentDidCatch(error: unknown, _errorInfo: unknown): void {
    // eslint-disable-next-line no-console
    console.log(&quot;TESTING. error : &quot;, error, &quot; , error info : &quot;, _errorInfo);
    Sentry.captureException(error);
  }

And here's an example of log inside this function :
> Error: Objects are not valid as a React child(...)

This error is not captured by Sentry while, in my mind, it should be.

Edit :
I might not have configured Sentry properly locally since I have Cors error when trying to debug and even my captureMessage() don't reach Sentry.

答案1

得分: 1

我建议您检查您的 _error.js 文件是否有任何排除项。例如,您可能没有记录 404 错误。

MyError.getInitialProps = async (context) => {
  const errorInitialProps = await NextErrorComponent.getInitialProps(context);
  
  const { res, err, asPath } = context;

  errorInitialProps.hasGetInitialPropsRun = true;

  // 返回提前,因为我们不希望将 404 错误记录到 Sentry。
  if (res?.statusCode === 404) {
    return errorInitialProps;
  }

  //...
};

您还可以检查您的初始化方法。Sentry 有 'ignoreErrors' 选项。

Sentry.init({
  dsn: SENTRY_DSN ,
  release: SENTRY_RELEASE,
  tracesSampleRate: SENTRY_SAMPLING_RATE,
  // 检查是否有任何排除项
  ignoreErrors: [/The user is not authenticated/]
});

最后但并非最不重要的是,检查 whitelistUrls。例如:

Raven.config('your-dsn', {
    whitelistUrls: [
        'www.example.com/static/js', // 您的代码
        'ajax.googleapis.com'        // 从 Google CDN 提供的代码
    ]
}).install();

这个示例配置确保只有来自 www.example.com/static/js 和 ajax.googleapis.com 的脚本引发的错误会报告给 Sentry 服务器。这个小配置更改是您可以做的最简单、最有影响力的改变,以减少错误。

来源:https://blog.sentry.io/tips-for-reducing-javascript-error-noise/

英文:

I can advise you to check your _error.js file for any exclusions. For instance, you may not be logging 404 errors.

MyError.getInitialProps = async (context) =&gt; {
  const errorInitialProps = await NextErrorComponent.getInitialProps(context);
  
  const { res, err, asPath } = context;

  errorInitialProps.hasGetInitialPropsRun = true;

  // Returning early because we don&#39;t want to log 404 errors to Sentry.
  if (res?.statusCode === 404) {
    return errorInitialProps;
  }

  //...
};

You may also want to check your init method. Sentry has 'ignoreErrors' option.

Sentry.init({
  dsn: SENTRY_DSN ,
  release: SENTRY_RELEASE,
  tracesSampleRate: SENTRY_SAMPLING_RATE,
  // check for any exclusions
  ignoreErrors: [/The user is not authenticated/]
});

Last but not least, check for whitelistUrls. E.x.

Raven.config(&#39;your-dsn&#39;, {
    whitelistUrls: [
        &#39;www.example.com/static/js&#39;, // your code
        &#39;ajax.googleapis.com&#39;        // code served from Google CDN
    ]
}).install();

> This example configuration ensures that only errors that originate from scripts served from www.example.com/static/js and ajax.googleapis.com are reported to the Sentry server. This small configuration change is the easiest, most impactful change you can make to reduce errors.

Source: https://blog.sentry.io/tips-for-reducing-javascript-error-noise/

答案2

得分: 1

Sentry应该捕获所有错误,但已经在服务器端对某些React错误进行了过滤,这可能会产生很多噪音,但不提供任何真正的价值,例如,水合错误。

为了确保SDK准备好捕获您的错误,您应首先检查Sentry.init()是否在您的应用程序中的任何其他代码之前调用。此外,您可以打开debug选项以查看日志。

英文:

Sentry should be capturing all errors but has server-side filters for some react errors in place that can be quite noisy but don't provide any real value, for example, hydration errors.

To make sure that the SDK is ready to capture your errors your should first check whether Sentry.init() is called before any other code in your app. Additionaly, you can turn on the debug option for logs.

huangapple
  • 本文由 发表于 2023年6月29日 00:02:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76574920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定