question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Errors in rejected promises not handled

See original GitHub issue

Hi I’m not sure if this is expected or not but we have errors happening inside a try/catch in a promise where the catch rejects the promise with the caught error. Chrome shows these as errors in the dev console, but Bugsnag doesn’t seem to get them.

I put together a JS fiddle with bugsnag loaded in to demonstrate the error: https://jsfiddle.net/zz050pg4/2/ . If you just put in your bugsnag api key on the first line, you can see that it notifies correctly for errors outside the promise. The promise code below is in a library and is basically:

var errPromise = new Promise(function (resolve, reject) {
    try {
        // call some of our source code to cause error
    } catch (err) {
        reject(err);
    }
})

Any thoughts?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
jmshalcommented, Jan 26, 2017

IMO, this is a bit inconvenient. I’d prefer if the uncaught exception thing would trigger when there’s no explicit catch

I agree. There is no reason why the notifier cannot hook into the unhandledrejection event.

I will submit a PR this evening soon, unless anybody wants to beat me to it - or has any other ideas.

Shout out to @jacobmarshall for the good explanation and suggestion, though.

You’re most welcome 😃

1reaction
jmshalcommented, Jun 18, 2015

This is because you are catching the errors yourself. Promises automatically reject if a thrown error is uncaught. If you need the caught error to be passed to Bugsnag, I would recommend doing something along the lines of…

new Promise(function (resolve, reject) {
    try {
        // throw an error
    } catch (err) {
        reject(err);
        Bugsnag.notifyException(err);
    }
});

or, better yet…

new Promise(function (resolve, reject) {
    throw new Error('Whoops, I threw a bad error!');
})
.then(function () { /* Won't ever get here... */ })
.catch(Bugsnag.notifyException);

It turns out that Bugsnag.notifyException is compatible with promises, because the first argument accepts an Error, and promises only pass one argument to then and catch.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is an unhandled promise rejection? - Stack Overflow
A rejected promise is like an exception that bubbles up towards the application entry point and causes the root error handler to produce...
Read more >
Error handling with promises - The Modern JavaScript Tutorial
In case of an error, the promise becomes rejected, and the execution should jump to the closest rejection handler. But there is none....
Read more >
Tracking Unhandled Promise Rejections - TrackJS
When a promise is rejected, it looks for a rejection handler. If it finds one, like in the example above, it calls the...
Read more >
Handling those unhandled promise rejections with JS async ...
In the first case, the key is this part of the error message: … by rejecting a promise which was not handled with...
Read more >
What is UnhandledPromiseRejectionWarning
catch(). A rejected promise is like an exception that bubbles up towards the application entry point and causes the root error handler to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found