How to handle exceptions?

Angular has a great $ exceptionHandler . Is there anything similar for response.js?

I would like to register my errors in an external API. Examples:

+8
exception-handling reactjs error-handling
source share
2 answers

Angular has no magic. If there is an uncaught error, it spreads over areas until it hits the window, and then an error event is issued - the same behavior as the error, without reaction.

var App = React.createClass({ render: function(){ throw new Error("rawr!"); return <div>Hello, world!</div> } }); window.addEventListener('error', function(e){ // e instanceof ErrorEvent console.error('caught the error: ' + e.message); }); 

chrome console displaying the error

If you look at cross-browser support, update this answer or add a comment with quotes.

Instead of writing it to the console (which is already happening by default), you can send it anywhere.

Unlike some other frameworks, an uncaught error is always an error in your code. This should never happen.

You may also need special handling of promise errors by explicitly adding .catch(reportError) to the end of the chain and checking that it is a TypeError or ReferenceError.

+15
source share

While @FakeRainBrigand's answer provides a very good starting point in understanding Javascript's general mechanism for reporting “uncaught error”, I feel like most applications host their JS packages using CDNs where you simply cannot capture errors, window.addEventListener ('error', fn) ".

Bypass Crossorigin restrictions

Put the crossorigin attribute in your script tag so you can enjoy catching uncaught errors with "window.addEventListener ('error', fn)".

In cases where you just can't get around the cors settings

React 16: provides an integrated error handling mechanism. Read more about Errors in Action here.

React 15 or lower: The only way to catch errors here is to add try-catch blocks around each render / component. Fortunately, we have the npm module, react-guard , which does just that for us, fixing React.

0
source share

All Articles