Disabling automatic error capturing in JavaScript SDK

Hello,

I have a JavaScript script that needs to be embedded on hundreds of different websites, and I am using Sentry to tell if something goes wrong. I have the following code:

import * as Sentry from '@sentry/browser';

Sentry.init({
    dsn: 'MY_SENTRY_DSN',
    environment: 'MY_ENV'
});

try {
    // Do stuff
} catch (error) {
    Sentry.captureException(error);
}

I want to capture only the errors that are thrown within the try-catch block. Right now, I get spammed with errors that happen in other scripts on the websites - i.e. if any JavaScript error occurs. I only want to capture errors that occur within my script, so the ones I capture manually. I suppose Sentry is hooking into JavaScript’s error handler and that’s why it captures a lot of stuff that’s not relevant to me. That’s fine, but I just need a way to disable it. I couldn’t find any configuration option that does this. How can I accomplish this?

Thanks in advance!

I think what you want is

defaultIntegrations: false

Sentry.init({
    dsn: 'MY_SENTRY_DSN',
    environment: 'MY_ENV',
    defaultIntegrations: false
});

This also disables breadcrumb tracking and patching of global functions like console.

That way you will only capture Exception you explicitly catch yourself.

Hey HazAT,

Thanks for the hint! After a bit of digging, I figured out that I can override the settings of integrations by instantiating them and adding them to the integrations key, like this:

Sentry.init({
    dsn: 'MY_SENTRY_DSN',
    environment: 'MY_ENV',

    integrations: [
        new Sentry.Integrations.GlobalHandlers({
            onerror: false,
            onunhandledrejection: false
        })
    ]
});

It appears to work how I expected; an error handler is no longer added to window.onerror, and breadcrumbs etc. still work. That’s the best case scenario, since the default integrations are still useful. I have yet to test it in production, but so far it seems to work.

Thanks a lot for pointing me in the right direction! :slight_smile:

1 Like

To my surprise, the above solution turned out not to work. That’s really weird, since window.onerror no longer gets set by Sentry.init(). Nevertheless, I am still seeing errors from other scripts being reported to Sentry. Back to square one.

Hi.
Did you find a solution?

Nope. I ended up doing the following.

Sentry.init({
    integrations(integrations) {
        return integrations
            // Disable global error handler
            .filter(i => i.name !== 'GlobalHandlers')

            // Disable other integrations
            .filter(i => i.name !== 'TryCatch');
    }
});

I still get errors from other scripts, but just haven’t had time to look more into it, although it’s really inconvenient still.