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?
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.
Thanks for the reply! Yeah that's what I was thinking we'd need to do but since Chrome showed the caught error I thought there might be another way. Unfortunately the try/catch is in another library's code and they don't provide any hooks for error handling.
IMO, this is a bit inconvenient. I'd prefer if the uncaught exception thing would trigger when there's no explicit catch
import Bugsnag from 'bugsnag-js' // add uncaught exception reporting
new Promise(() => { throw new Error('i wish this were reported to Bugsnag'); });
new Promise.reject().catch(() => console.log('makes sense not to report this, though'));
throw new Error('this is reported to Bugsnag');
Now, I pretty much have to add a Bugsnag.notifyException on every Promise in the codebase. In fact, it has to look more like
everyPromiseEver.catch((e) => { Bugsnag.notifyException(e); console.error(e) });
if I want the error to not be totally swallowed while I'm tinkering in development.
Shout out to @jacobmarshall for the good explanation and suggestion, though.
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 :)
I like the idea of having a listener for unhandledrejection, but it's worth mentioning that it has very poor cross-browser support right now, and unfortunately I don't see a way to polyfill it that would be in scope for bugsnag-js. Since we don't have much single-browser functionality in bugsnag-js, I'm wondering that the best way would be to make clear to people that this feature is single-browser. I wouldn't want them to see only rejections for Chrome and think that their app is somehow more broken in Chrome than any other browser. Maybe have it off by default, and then have a prominent note about browser support next to the docs for how to turn it on?
Aside: If you want to listen globally for unhandled rejections across browsers, it may be worth checking out bluebird or another library that will set this up — then you can notify Bugsnag in the rejection handling function.
Ah sorry @jacobmarshall, just saw that you opened #221 — we can continue the discussion over there!
Most helpful comment
I agree. There is no reason why the notifier cannot hook into the
unhandledrejectionevent.I will submit a PR
this eveningsoon, unless anybody wants to beat me to it - or has any other ideas.You're most welcome :)