DEV Community

Discussion on: When a Promise falls in your app and no one is there to catch it, does it error? πŸ€”

Collapse
 
stefandorresteijn profile image
Stefan Dorresteijn
window.onunhandledrejection = function (error) {
    console.error(`Promise failed: ${error.reason}`);
};

Please, please, please don't do this. This to me is exactly as bad as putting your entire application in a try, catch. You'll miss important errors and it'll make debugging a hell. Let your application crash, catch it with a good error reporting tool, and fix it. This is why we run betas and test often.

Collapse
 
aaronpowell profile image
Aaron Powell

You're right, you should have proper error handling as close to where you expect an error to occur and use a monitoring tool (which I blogged about recently) to capture unhandled errors.

The code illustrated in this post is just what these tools do, they attach to the global events like onunhandledrejection and onerror.

The point of this post is to show you how those work so you have a better understanding of just what is happening in the libraries you bring into a project.

Yes, you shouldn't capture a global event and just console.error it, you should do something constructive with it, but the code is for illustrative purposes. 😊