Sentry-javascript: No way to capture exceptions without installing a global error handler

Created on 9 Feb 2015  路  11Comments  路  Source: getsentry/sentry-javascript

Raven.captureException() won't work properly when Raven.install() has not been called beforehand; calling Raven.install() will install a global error handler (window.onerror). This is problematic if one wants to catch errors with a custom handler and pass them to Raven by hand, e.g. to lazy-load Raven as needed.

Most helpful comment

Closing this because Raven.captureException works without calling .install today.

All 11 comments

I've run into this as well. Basically we want to only use Raven.captureException() and ignore all exceptions that are not explicity handled by our code. But we get a lot of random other things in as well.

It seems that collectWindowErrors: false in options should be the way to avoid this but it doesn't work.

After digging around the code it appears TraceKit automatically installs an error handler when TraceKit.report.subscribe() is called. If I remove the call to installGlobalHandler() I can still capture exceptions with Raven.captureException() but global errors are not caught.

I'm not sure that would even be the correct solution but would it be possible to add a configuration option to avoid TraceKit installing that global error handler?

This is really important to us as well, +1

Can any of you elaborate on "won't work properly"? Perhaps with a test case / JSBin example?

Here is my use case:

  • catch inline exceptions manually _only_

What configuration allows this to happen? I don't want any uncaught errors, but I want captureException to report something.

Here is the relevant jsbin: https://jsbin.com/kugowopeso/edit?html,js,output

So I'm a paying customer of sentry, and I took the time to elaborate on my issue...care to elaborate on your solution? :smile:

So I'm a paying customer of sentry

In the future you should use the support email link from inside your account. This verifies you have an account with a support tier, and puts your issue inside our support queue where it gets prioritized / assigned.

care to elaborate on your solution? :smile:

The JSBin example you've provided calls install(), and appears to work as documented (every exception is caught and reported).

This edited version drops the call to install(), and works the way I believe you're intending (only manual calls to captureException are reported).

If this isn't what you're looking for, I strongly recommend moving this outside of GitHub and into a support thread. Just link to this issue and we can continue the conversation.

Thanks Ben, didn't mean to be snarky there - and you're absolutely right, your solution fixes the problem outlined. Unfortunately I forgot an essential detail, so let's try this again:

https://jsbin.com/bojasu/2/edit?html,js,output

My use case is _actually_:

  • catch inline exceptions manually
  • catch exceptions in my source files
  • do not catch global exceptions
  • do not catch exceptions in other people's source files

I appreciate us keeping this an issue on github, I think it may help others!

To raven-js there is no difference between:

<script>
  throw new Error('Do not catch this')
</script>

And an external script that throws an uncaught exception, e.g.:

<!-- this one throws and I want to catch it (my application files for example) -->
<script src="https://gist.githubusercontent.com/henrybaxter/af28cceb30b4bda21a55/raw/4c5f2891bcf3011221b6b6572ffd8fc29eeb042e/throws.js"></script>

... where throws.js contains throw new Error('catch this'). They are both uncaught exceptions; the origin of the file makes no difference. You need to use try/catch in both examples. (Again, assuming install() has been omitted.)

If your goal is to globally catch all of your exceptions, but not exceptions occurring from _other_ people's source files, you should use the whitelistUrls or ignoreUrls configuration options and call install().

Ok so just for the record, what I'm asking for is not possible, right? Because

  1. install() plus whitelisting main page and my external source files => I get uncaught exceptions on my main page
  2. install() plus whitelisting only my external source files => I miss _manually_ caught exceptions on my main page
  3. omit install() => I miss uncaught exceptions in my external source files

For example, if I'm writing a tracking script or a split testing service, I want to report only on the things I do wrong, not what else is going on on the page, nor the endless possible list of first or third party source files included in the page that I cannot know about.

install() plus whitelisting only my external source files => I miss manually caught exceptions on my main page

If you mean exceptions would be excluded from _inline scripts_, then yes. All your code is running "on your main page".

For example, if I'm writing a tracking script or a split testing service, I want to report only on the things I do wrong, not what else is going on on the page, nor the endless possible list of first or third party source files included in the page that I cannot know about.

There are many users of raven-js/Sentry that use this scenario. They whitelist their own script URLs. If your tracking script is wholly embedded inline inside a <script/> tag, that will be trickier. But you really should not be encouraging users to embed much (if any) code inside an inline <script/> tag. For example, the Google Analytics script snippet is like 10 lines of code, which simply pulls down an external script that contains most of your application.

In any case, I think this needs to go to another GitHub issue (if not a support ticket), because I don't think what you're describing matches the original issue (that Raven.captureException "doesn't work" without first calling install()).

Closing this because Raven.captureException works without calling .install today.

Was this page helpful?
0 / 5 - 0 ratings