Catch unhandled promise errors

Time:2020-11-19

Translator’s note:By monitoringunhandledrejectionEvent to catch unhandled promise errors.

  • Tracking unhandled rejected projects
  • Translator: fundebug

In order to ensure the readability, this paper uses free translation instead of literal translation, and makes a lot of modifications to the source code. In addition, the copyright of this paper belongs to the original author, and the translation is only used for learning.

usePromiseWhen writing asynchronous code, userejectTo handle errors. Sometimes, developers often ignore this, resulting in some errors not being handled. For example:

function main() {
    asyncFunc()
    .then(···)
    .then(() => console.log('Done!'));
}

Because it is not usedcatchMethod to catch errors whenasyncFunc()functionrejectThe error thrown is not handled.

This blog will introduce the browser and Node.js How to catch those unprocessed promise errors.

Unhandled promise error in browser

Some browsers, such as chrome, can catch unprocessed promise errors.

unhandledrejection

monitorunhandledrejectionEvent to catch an unhandled promise error:

window.addEventListener('unhandledrejection', event => ···);

This event isPromiseRejectionEventInstance, which has two most important properties:

  • promise: reject’s promise
  • reason: reject value of project

Example code:

window.addEventListener('unhandledrejection', event =>
{
    console.log ( event.reason ); // print "Hello, fundebug!"
});

function foo()
{
    Promise.reject('Hello, Fundebug!');
}

foo();

Fundebug’s JavaScript error monitoring plug-in is listeningunhandledrejectionEvent, so you can automatically catch unhandled promise errors.

rejectionhandled

A promise error is triggered when it is not initially handled but is later processedrejectionhandledevent:

window.addEventListener('rejectionhandled', event => ···);

This event isPromiseRejectionEventexample.

Example code:

window.addEventListener('unhandledrejection', event =>
{
    console.log ( event.reason ); // print "Hello, fundebug!"
});

window.addEventListener('rejectionhandled', event =>
{
    console.log ('rejection handled '); // print "rejection handled" in 1 second
});


function foo()
{
    return Promise.reject('Hello, Fundebug!');
}

var r = foo();

setTimeout(() =>
{
    r.catch(e =>{});
}, 1000);

Node.js Unhandled promise error in

monitorunhandledRejectionEvent to catch an unhandled promise error:

process.on('unhandledRejection', (reason, promise) => ···);

Example code:

process.on('unhandledRejection', reason =>
{
    console.log (reason); // print "Hello, fundebug!"
});

function foo()
{
    Promise.reject('Hello, Fundebug!');
}

foo();

Note:Node.js V6.6.0 + reports unprocessed promise errors by default, so it does not listenunhandledrejectionThere’s no problem with the incident.

Fundebug’s Node.js The error monitoring plug-in is listeningunhandledRejectionEvent, so you can automatically catch unhandled promise errors.

reference resources

  • Promise Rejection Events Sample
  • Event: ‘unhandledRejection’

About fundebug

Fundebug focuses on JavaScript, WeChat applet, WeChat games, Alipay applet, React Native, Node.js And java online application real-time bug monitoring. Since the official launch of “double 11” in 2016, fundebug has handled a total of 1 billion + error events. Paying customers include Google, 360, Jinshan software, renmin.com and many other brand enterprises. Welcome to try for free!

Catch unhandled promise errors

Copyright notice

Please indicate the author fundebug and the address of this article:
https://blog.fundebug.com/2017/10/09/unhandled-pomise-rejection/