sentry-javascript: Support for "Unhandled promise rejection" / "Uncaught (in promise)"
Example use case (JSBin):
var p = new Promise(function(resolve, reject) {
window.setTimeout(
function() {
reject();
}, 10);
});
p.then(function () {
console.log('done!');
});
If the promise throws an exception, and there’s no error handler, the browser will spit out an uncaught promise error.

About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 18
- Comments: 41 (19 by maintainers)
Commits related to this issue
- Change unhandledrejection docs example to use assignment (see #424) — committed to getsentry/sentry-javascript by benvinegar 8 years ago
- ref: Use a named function for the middleware over an anonymous (#424) It's making the investigation of performance issues easier. — committed to getsentry/sentry-javascript by oliviertassinari 6 years ago
what’s the TL;DR solution here?
@joaocunha – looking at the code above, it looks like you could do the following as a workaround:
Just a heads up for anyone coming across this thread. There is a bug in Chrome 51 and higher (as recent as Chrome 56) where unhandled promise rejections are never fired on localhost. If you’re testing locally then you may never see this event fired!
See: https://bugs.chromium.org/p/v8/issues/detail?id=4874
I’m using Babel (which uses core-js), to get it to work there I’ve had to:
delete window.Promiseat the very start of the app, before loading any other code to ensure core-js’s Promise polyfill is used insteadwindow.onunhandledrejection = ..instead ofwindow.addEventListener('unhandledrejection', ..)My handler looks like:
And to delete the native Promise, I’ve got a file
src/disableNativePromises.jsand I’m loading that as the first file in my
entryconfig for Webpack:Just updated the issue above about Chrome. It doesn’t work for me in Chrome 56 if webpack
devtooloption is set toeval, which was the default setting for local development.@jessepollak – Raven.js wraps async methods in try/catch for uncaught exceptions (literally
throw X), but that does not cover uncaught promises. The stack trace via the console still shows the wrap, though.@MarkMurphy – We’re going to make it a config option soon-ish, but in the meantime you can implement yourself pretty easily:
https://docs.sentry.io/clients/javascript/usage/#promises
Just a note. If you are using Redux-Thunk with async/await, you can catch these unhandled promise rejections by catching redux-thunk errors manually.
code here https://gist.github.com/akhayoon/24be6ca9634d228893b8c4bd79f0f33c
https://github.com/zloirock/core-js/issues/140 was resolved, though I wouldn’t close this issue until a suitable workaround can be automated. awesome project!
@joaomilho – So, I played around with this, and the thing is,
reasonis non-standard and could contain anything. It could be a string, it could be an error, etc.The examples from MDN actually demonstrate passing both an error and a string: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject
The problem here is that
Raven.captureExceptionpurely capturesErrorobjects, andRaven.captureMessagecaptures strings.Perhaps the best solution is:
We could also introduce a function, say,
Raven.capturethat tries to call the appropriate function based on the type of the passed arguments.Edit: @mattrobenolt points out that
Raven.captureExceptionautomatically falls back to messages, so this if statement is not necessary. Just usecaptureException.@joaocunha – so @mattrobenolt and I have had a discussion about this. It seems like
Reactyour module loader has wrapped loading the module in a try/catch, and instead of re-throwing theReferenceError, it passes it down via the Promise rejection. Because the error isn’t actually thrown, ouronerrorhandler doesn’t catch it and we instead get thisUnhandled promise.So … we feel this might be a
Reactmodule-loader-specific thing.We’re going to dig further into React, make sure we have a good handle on this, then get back to you.If anyone has similar examples re: uncaught promise rejections, we’d love to hear 'em.