38

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?

Finlay Percy
  • 6,763
  • 4
  • 22
  • 28
serverpunk
  • 10,665
  • 15
  • 61
  • 95
  • Have you checked out the [usage](https://docs.sentry.io/clients/javascript/usage/) pages in the docs? – mausworks Nov 16 '17 at 23:02
  • 2
    Feel free to review the answers and update the accepted one if need be, others may appreciate it! – Leo Dec 23 '19 at 13:17
  • 1
    console - document.body.addEventListener('click', function() { throw new Error('Sentry Test'); }); - and then just click anywhere – sunn0 Mar 30 '22 at 14:06

5 Answers5

30

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.

adius
  • 13,685
  • 7
  • 45
  • 46
  • Awesome! that is neat. exactly what I was looking for. I'm using react Integration BTW, and it worked well. – M. Amer Mar 08 '22 at 19:52
  • This is fantastic. Worked perfect. One thing to keep in mind is you might have to write all of this code in the console. So, you'll need to use `document.createElement`, `Element.setAttribute("onclick")` and `Element.appendChild`. – dimiguel Mar 23 '22 at 23:49
  • This was super helpful - throwing errors from the console directly didn't trigger Sentry, but manually adding an element into the DOM via DevTools and throwing an unhandled error from there was perfect. If anyone struggles with this, try right-clicking a random `div` element and `Edit as HTML`, then add the answer's element with the `onClick` event. – bsplosion Feb 17 '23 at 21:05
  • This one should be the answer, as it's correctly testing that Sentry is reporting errors. – Mgs M Rizqi Fadhlurrahman Feb 23 '23 at 13:28
  • Simple. Elegant. Works. Best answer. – ksh Aug 02 '23 at 16:48
24

Verify (newer)

myUndefinedFunction();

Verifying Your Setup (older)

Sentry.captureException(new Error("This is my fake error message"));

https://docs.sentry.io/platforms/javascript/?platform=browser#verifying-your-setup

May be worth double-checking your setup (or updating) and config too.

Leo
  • 10,407
  • 3
  • 45
  • 62
2

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.

Finlay Percy
  • 6,763
  • 4
  • 22
  • 28
1

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.

ehfeng
  • 3,807
  • 4
  • 33
  • 42
1

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 ;)

Darren Shewry
  • 10,179
  • 4
  • 50
  • 46