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 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:
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!
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.
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.