Skip to content

Commit

Permalink
[client-app] Correctly handle window.unhandledrejection events
Browse files Browse the repository at this point in the history
Summary:
It seems that sometimes we get events that don't have `event.detail.reason`.
This commits tries to make our best guess, or report info about the event shape

Test Plan: manual

Reviewers: mark, halla

Reviewed By: halla

Differential Revision: https://phab.nylas.com/D4420
  • Loading branch information
jstejada committed Apr 13, 2017
1 parent aa7749d commit 911d75b
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/client-app/src/nylas-env.es6
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,30 @@ export default class NylasEnvConstructor {
// caught by `process.on('unhandledRejection')`, so we listen for unhandled
// rejections on the`window` as well
window.addEventListener('unhandledrejection', e => {
const error = e.detail.reason
// This event is supposed to look like {reason, promise}, according to
// https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent
// In practice, it can have different shapes, so we try to make our best
// guess
if (!e) {
const error = new Error(`Unknown window.unhandledrejection event.`)
this._onUnhandledRejection(error, sourceMapCache)
return
}
if (e instanceof Error) {
this._onUnhandledRejection(e, sourceMapCache)
return
}
if (e.reason) {
const error = e.reason
this._onUnhandledRejection(error, sourceMapCache)
return
}
if (e.detail && e.detail.reason) {
const error = e.detail.reason
this._onUnhandledRejection(error, sourceMapCache)
return
}
const error = new Error(`Unrecognized event shape in window.unhandledrejection handler. Event keys: ${Object.keys(e)}`)
this._onUnhandledRejection(error, sourceMapCache)
});

Expand Down

0 comments on commit 911d75b

Please sign in to comment.