Catch unhandled Promise errors by listening for unhandledrejection events.
- Tracking unhandled rejected Promises
- Translator: Fundebug
In order to ensure readability, free translation rather than literal translation is used in this paper, and a lot of modifications are made to the source code. In addition, the copyright of this article belongs to the original author, and translation is for study only.
When writing asynchronous code with Promise, use reject to handle errors. Sometimes, developers often overlook this and errors go unhandled. Such as:
Function main() {asyncFunc().then(···).then(() => console.log('Done! ')); }Copy the code
Because no catch method is used to catch errors, the asyncFunc() function reject throws errors that are not processed.
This blog post will show you how to catch unhandled Promise errors in the browser and node.js, respectively.
Unhandled Promise error in browser
Some browsers, such as Chrome, can catch unhandled Promise errors.
unhandledrejection
Listen for unhandledrejection events to catch unhandled Promise errors:
Window. The addEventListener (' unhandledrejection 'event = >...);Copy the code
This event is an instance of PromiseRejectionEvent, which has two of the most important properties:
promise
Promise: rejectreason
: Reject value of Promise
Sample code:
window.addEventListener('unhandledrejection', event => { console.log(event.reason); // Prints "Hello, Fundebug!" }); function foo() { Promise.reject('Hello, Fundebug! '); } foo();Copy the code
Fundebug’s JavaScript error monitoring plug-in listens for unHandledrejection events, so it can automatically catch unprocessed Promise errors.
rejectionhandled
When a Promise error is not handled initially, but is later handled, the rejectionHandled event is raised:
Window. The addEventListener (' rejectionhandled 'event = >...);Copy the code
This event is an instance of PromiseRejectionEvent.
Sample code:
window.addEventListener('unhandledrejection', event => { console.log(event.reason); // Prints "Hello, Fundebug!" }); window.addEventListener('rejectionhandled', event => { console.log('rejection handled'); // After 1 second print "rejection handled"}); function foo() { return Promise.reject('Hello, Fundebug! '); } var r = foo(); setTimeout(() => { r.catch(e =>{}); }, 1000);Copy the code
Unhandled Promise error in Node.js
Listen for unhandledRejection events to catch unhandled Promise errors:
Process. on('unhandledRejection', (reason, promise) => ···);Copy the code
Sample code:
process.on('unhandledRejection', reason => { console.log(reason); // Prints "Hello, Fundebug!" }); function foo() { Promise.reject('Hello, Fundebug! '); } foo();Copy the code
Note: Node.js v6.6.0+ reports unhandled Promise errors by default, so it’s ok not to listen for unhandledrejection events.
Fundebug’s Node.js error monitoring plug-in listens for unhandledRejection events, so it can automatically catch unprocessed Promise errors.
reference
- Promise Rejection Events Sample
- The Event: ‘unhandledRejection’
Are your users experiencing bugs?
Experience the Demo
Free to use