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!

1 Like

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:

2 Likes

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.

1 Like

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.

1 Like

Related: https://docs.sentry.io/platforms/javascript/#trycatch

Thanks! I had removed that since my last post, though, and unfortunately without success. I have the following:

Sentry.init({
    // ...
    integrations(integrations) {
        return integrations
            .filter(i => i.name !== 'GlobalHandlers')
            .filter(i => i.name !== 'Breadcrumbs')
            .filter(i => i.name !== 'TryCatch');
    }
});

Looking over the integrations again, I am still not sure what causes a lot of junk to be captured.

1 Like

I can confirm that none of these solutions are working and GlobalHandlers is always enabled. I guess there’s a bug in the sentry’s browser package.

I tried everything and none of them work. I either have all of the integrations or none of them! There’s no way to disable a single integration.

So I came up with this workaround:

const hub = new Hub(); // cause our project is embedded in another project
new BrowserClient({
    dsn: '__DSN__',
    beforeSend(event: Event, hint?: EventHint): PromiseLike<Event | null> | Event | null {
        if (!event.extra?.myFlag) return null;
        return event;
    },
})
hub.bindClient(client);

...

hub.withScope(scope => {
    scope.setExtra('myFlag', true);
    hub.captureMessage(message, severity);
});

I wish sentry could pay more attention on resolving such bugs and github’s open issues