0

I have this simple code at the start of my ts file

//window.onerror = function() {
//    alert('err')
//};

window.addEventListener('error', function (event) {
alert(event.message)
})

As you can see, I've tried both syntaxes.

In another function later on I have:

throw new Error('dummy err');

But the global listener does not fire

What can the issue be?

Chrome shows the the global listener to this line. but nothing happens

I am using React. Though I dont see React to be touching this

How do I ensure that all thrown errors get to my top global handler?

BTW: even regular errors, that are thrown lets say by fetch which get back 500 errors, are also not caught by the global handler. Whats wrong?

Thanks!

Yisroel M. Olewski
  • 1,560
  • 3
  • 25
  • 41
  • You can use react`ErrorBoundary https://reactjs.org/docs/error-boundaries.html` to catch all errors. Though above behaviour is weird. – Rabi jha Sep 11 '21 at 20:00

1 Answers1

1

As soon as I realized that the errors that were not caught were only from promises / async. I quickly found the answer here https://stackoverflow.com/a/55178672/240742

Yisroel M. Olewski
  • 1,560
  • 3
  • 25
  • 41