Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give better stack traces for Unhandled Promise Rejection warnings #60235

Merged
merged 1 commit into from
Mar 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/setup_node_env/exit_on_warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ if (process.noProcessWarnings !== true) {

process.exit(1);
});

// While the above warning listener would also be called on
// unhandledRejection warnings, we can give a better error message if we
// handle them separately:
process.on('unhandledRejection', function(reason) {
console.error('Unhandled Promise rejection detected:');
console.error();
console.error(reason);
console.error();
console.error('Terminating process...');
process.exit(1);
});
Comment on lines +42 to +49
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this already catched by the warning event?

From https://kibana-ci.elastic.co/blue/rest/organizations/jenkins/pipelines/elastic+kibana+pipeline-pull-request/runs/33334/nodes/28/steps/147/log/?start=0

Node.js process-warning detected:
[2020-03-16T08:30:26.360Z] 
[2020-03-16T08:30:26.360Z] UnhandledPromiseRejectionWarning: [object Object]
[2020-03-16T08:30:26.360Z]     at emitWarning (internal/process/promises.js:99:15)
[2020-03-16T08:30:26.360Z]     at emitPromiseRejectionWarnings (internal/process/promises.js:143:7)
[2020-03-16T08:30:26.360Z]     at process._tickCallback (internal/process/next_tick.js:69:34)
[2020-03-16T08:30:26.360Z] 
[2020-03-16T08:30:26.360Z] Terminating process...
[2020-03-16T08:30:26.360Z] Fatal error: jest exited with code 1�

Or will the reason catched by the unhandledRejection contains more info than what is catched by the warning handler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The stack trace available for the unhandled exception is just pointing back to a next-tick frame which doesn't show any of the user-land code. This makes the stack trace unusable and the reader can't see which Promise was not handled correctly.

By implementing an unhandledRejection listener we get access to the rejection reason, which hopefully always is an Error object with its own stack trace. We can use this stack trace to guide the developer towards the location where the Promise rejection wasn't handled.

Copy link
Contributor

Choose a reason for hiding this comment

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

That was the question :) great then.

}