How do I test that Sentry is reporting errors?

Viewed 25821

I just installed Sentry for a client-side JavaScript app using the standard code snippet they provided. How do I test that it's working properly? I've tried manually throwing an error from my browser console and it didn't appear in Sentry. Is there any documentation on the right way to do this?

5 Answers

The browser console can not be used as it is sandboxed. A simple trick is to attach the code to an HTML element like this:

<h1 onClick="throw new Error('Test')">
  My Website
</h1>

And click on the heading afterwards.

This can be done in the browser inspector and so your source code doesn't have to be modified.

One way to generate an error in Sentry is to call a function that is not defined.

Note: This cannot be done in the console - it must be in the code.

Try adding this to your code (taken from the docs):

myUndefinedFunction();

If your code build doesn't allow this due to tests/linting you might be able to use:

window.myUndefinedFunction()

You should then see error in your browser console and in the Sentry dashboard.

Have a read of the Docs for more info.

Raven.captureMessage('Broken!') is a good place to start (also pulled from Sentry docs). If that fails to send, the Raven client isn't being initiated.

If you can't or don't want to use an undefined function to test Sentry is sending errors, you can also use the Debugger in Chrome's DevTools by:

  1. Adding a breakpoint in your code e.g. for a click event handler
  2. Trigger that event e.g. click a button
  3. When the breakpoint hits in the console, set a function/variable that is needed for execution to undefined
  4. Press play/continue in DevTools to then see the Error output

e.g.

1: function onClick() {
2:   api.sendSomeEvent();
3: }

Add a breakpoint in the body of the onClick event handler (Line 2), trigger the event. When execution is paused: in your console enter something like api = undefined and hit enter to update the state. Then continue execution (click the play button), where you should see an error (ala api is undefined) that Sentry should then send for you.

Note: this works for any environment, though you may need to be clever with finding your events in minified code ;)

Related