Skip to content

Ignoring errors from <anonymous> files #3147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
asbjornh opened this issue Jan 4, 2021 · 6 comments
Closed

Ignoring errors from <anonymous> files #3147

asbjornh opened this issue Jan 4, 2021 · 6 comments
Labels
Package: browser Issues related to the Sentry Browser SDK

Comments

@asbjornh
Copy link

asbjornh commented Jan 4, 2021

We're seeing a lot of errors that have stack traces like this:

image

Our website uses Google Tag Manager which injects tons of weird crashy stuff into our app that we don't know (or care) about.
I'm assuming that since there's no stack information available for these errors, they have arisen from injected code that for some reason has no accessible source URL (maybe inline or cross origin scripts?).

In any case, it seems like we're unable to do anything about these errors which means they essentially mount up to noise (and lots of it), so we would like to simply ignore these. I haven't been able to find any mention of how to ignore all errors without a source URL in the documentation (https://docs.sentry.io/platforms/javascript/configuration/).

@kamilogorek
Copy link
Contributor

kamilogorek commented Jan 28, 2021

It should be something along the lines of:

Sentry.init({
  beforeSend(event) {
    try {
      if (event.exception.values[0].stacktrace.frames[0].filename === `<anonymous>`) {
        return null;
      }
    } catch (e) {}

    return event;
  },
});

Please refer to https://github.com/getsentry/sentry-javascript/blob/master/packages/types/src/event.ts for more details on the event type itself.

Let me know you still need help. Cheers!

@asbjornh
Copy link
Author

asbjornh commented Feb 6, 2021

Great, thanks! I’ll try this 🙂

@asbjornh asbjornh closed this as completed Feb 6, 2021
@lobsterkatie
Copy link
Member

@asbjornh - I recognize that this isn't exactly your use case, but you might be interested in #3842 once it's released.

@lobsterkatie lobsterkatie added Package: browser Issues related to the Sentry Browser SDK Status: As Designed labels Jul 27, 2021
@asbjornh
Copy link
Author

asbjornh commented Aug 9, 2021

@lobsterkatie Thanks!

@christophehurpeau
Copy link

christophehurpeau commented Oct 27, 2023

Hello ! we've improved the beforeSend a bit and it might be usefull for someone, here it is:

/* disable errors originating from injected scripts such as Google Tag Manager */
function isInjectedCode(event: Sentry.Event | undefined): boolean {
  const frames = event?.exception?.values?.[0]?.stacktrace?.frames;

  if (!frames || frames.length === 0) return false;

  const firstFrame = frames[0];
  if (firstFrame.filename === '<anonymous>') {
    return true;
  }

  const lastFrame = frames[frames.length - 1];
  if (
    typeof lastFrame.filename === 'string' &&
    (lastFrame.filename === window.location.pathname ||
      (lastFrame.filename.startsWith(window.location.origin) &&
        // static file should not be considered as injected code. We use react-script currently, and all js-generated files are in this "static" directory.
        !lastFrame.filename.includes('/static/')))
  ) {
    return true;
  }

  if (
    frames.some(
      (frame) =>
        typeof frame.filename === 'string' &&
        (frame.filename.startsWith('http://www.googletagmanager.com') ||
          frame.filename.startsWith('https://googleads.')),
    )
  ) {
    return true;
  }

  return false;
}

Sentry.init({
    beforeSend(event, hint) {
      if (isInjectedCode(event)) {
        return null;
      }
      return event;
    },
});

@wvell
Copy link

wvell commented Nov 21, 2023

Hello ! we've improved the beforeSend a bit and it might be usefull for someone, here it is:

/* disable errors originating from injected scripts such as Google Tag Manager */
function isInjectedCode(event: Sentry.Event | undefined): boolean {
  const frames = event?.exception?.values?.[0]?.stacktrace?.frames;

  if (!frames || frames.length === 0) return false;

  const firstFrame = frames[0];
  if (firstFrame.filename === '<anonymous>') {
    return true;
  }

  const lastFrame = frames[frames.length - 1];
  if (
    typeof lastFrame.filename === 'string' &&
    (lastFrame.filename === window.location.pathname ||
      (lastFrame.filename.startsWith(window.location.origin) &&
        // static file should not be considered as injected code. We use react-script currently, and all js-generated files are in this "static" directory.
        !lastFrame.filename.includes('/static/')))
  ) {
    return true;
  }

  if (
    frames.some(
      (frame) =>
        typeof frame.filename === 'string' &&
        (frame.filename.startsWith('http://www.googletagmanager.com') ||
          frame.filename.startsWith('https://googleads.')),
    )
  ) {
    return true;
  }

  return false;
}

Sentry.init({
    beforeSend(event, hint) {
      if (isInjectedCode(event)) {
        return null;
      }
      return event;
    },
});

Thanks for your code. I changed http://www.googletagmanager.com to https://www.googletagmanager.com otherwise this does not work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Package: browser Issues related to the Sentry Browser SDK
Projects
None yet
Development

No branches or pull requests

5 participants