javascript 中停止抛出“超出 ResizeObserver 循环限制”错误避免超出 Sentry 配额的最佳方法是什么?

Ali*_*lov 5 javascript reactjs antd

在我的 ReactJs Web 应用程序中,我在特别使用AntTable. 我搜索了一下,据我所知这个错误是无害的,可以忽略。而且这种情况只发生在 Chrome 中,其他浏览器不会发生。但是,对我来说,问题是我已经集成Sentry到我的应用程序中来监视和处理错误。我的大多数用户都使用 Chrome 作为浏览器,因此我的哨兵报价很快就受到限制。忽略此特定错误的最佳方法是什么?现在,我正在尝试找到一些好的解决方案,而不是if向我的代码脚本添加脏语句引发错误以检查这是否是“ResizeObserver”错误。我只是不想这样做,是否还有其他解决方案?

如果需要,我还可以分享有关我的应用程序的任何其他详细信息 - 包括代码。

这是应用程序将捕获的错误发送到的函数Sentry

  init() {
    if (env.ENV === 'production' && !this.isInitialized) {
      this.isInitialized = true;
      Sentry.init({
        release: env.REACT_APP_VERSION,
        dsn: env.SENTRY_DSN,
        beforeSend(event) {
          const { request: { url }, exception: { values } } = event;
          if (
            url.includes('login') &&
            values[0].type === 'UnhandledRejection' &&
            values[0].value.includes('Non-Error promise rejection captured with keys')
          ) {
            return null;
          }

          return event;
        },
      });
    }
  }
Run Code Online (Sandbox Code Playgroud)

我尝试应用包装代码的解决方案requestAnimationFrame。但我不知道该如何退货。

  init() {
    if (!this.isInitialized) {
      this.isInitialized = true;
      Sentry.init({
        release: env.REACT_APP_VERSION,
        dsn: env.SENTRY_DSN,
        beforeSend(event) {
          const resizeObserver = new ResizeObserver((entries) => {
            window.requestAnimationFrame(() => {
              if (!Array.isArray(entries) || !entries.length) {
                return null;
              }
              return event;
            });
          });
          resizeObserver.observe(document.getElementById('app'));
        },
      });
    }
  }
Run Code Online (Sandbox Code Playgroud)

如果不是ResizeObserver错误的话,我只需要按原样返回。

pio*_*r.d 8

你的主要问题是

忽略此特定错误的最佳方法是什么?

您正在使用Sentry,并且希望防止报告不相关的错误以不超过配额。

在这种情况下,您应该ignoreErrors在应用程序中初始化 Sentry 时使用,如下所示:

Sentry.init({
  ignoreErrors: [
    "ResizeObserver loop limit exceeded"
  ],
  // other config options
  // ...
});
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅 Sentry 文档:https://docs.sentry.io/platforms/javascript/configuration/filtering/#decluttering-sentry

有关节省配额的更多想法,请阅读https://blog.sentry.io/2017/03/27/tips-for-reducing-javascript-error-noise(有点旧,但大多数提示仍然适用于新的 Sentry SDK)。