Skip to content

Commit

Permalink
Add support for promise rejection tracking
Browse files Browse the repository at this point in the history
Summary:
Adds support for tracking unhandled rejections with `console.warn` (= yellow box).

I will create a follow-up with proper error stack formatting.

related: #4971
fixes: #4045, #4142

public

{F59857438}

{F59857439}

Reviewed By: bestander

Differential Revision: D2803126

fb-gh-sync-id: 376b33e42a967675a04338cbff3ec315a77d1037
  • Loading branch information
davidaurelio authored and facebook-github-bot-3 committed Jan 7, 2016
1 parent 2b09614 commit b064094
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 102 deletions.
20 changes: 20 additions & 0 deletions Libraries/Promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@
global.setImmediate = require('setImmediate');
var Promise = require('promise/setimmediate/es6-extensions');
require('promise/setimmediate/done');
if (__DEV__) {
require('promise/setimmediate/rejection-tracking').enable({
allRejections: true,
onUnhandled: (id, error) => {
const {message, stack} = error;
const warning =
`Possible Unhandled Promise Rejection (id: ${id}):\n` +
(message == null ? '' : `${message}\n`) +
(stack == null ? '' : stack);
console.warn(warning);
},
onHandled: (id) => {
const warning =
`Promise Rejection Handled (id: ${id})\n` +
'This means you can ignore any previous messages of the form ' +
`"Possible Unhandled Promise Rejection (id: ${id}):"`;
console.warn(warning);
},
});
}

/**
* Handle either fulfillment or rejection with the same callback.
Expand Down
Loading

6 comments on commit b064094

@seidtgeist
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH MY GOD. Thank you!

@tomauty
Copy link
Contributor

@tomauty tomauty commented on b064094 Jan 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This slays.

@gre
Copy link
Contributor

@gre gre commented on b064094 Jan 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this.
is there a way to global catch this? instead of console.error. I was trying to use ErrorUtils.setGlobalHandler()

@gre
Copy link
Contributor

@gre gre commented on b064094 Jan 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ErrorUtils.reportError should be called for unhandled promise errors.

@davidaurelio
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will definitely make this configurable.

@slorber
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidaurelio as of now are we able to catch globally unhandled promise rejections?

This feature is handy to report errors to tools like Sentry. Also what is ErrorUtils, this seems undocumented anywhere?

See also #2585 (comment)

Please sign in to comment.