-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
process: Add --unhandled-rejections=throw and =warn-with-error-code #33475
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Flags: --unhandled-rejections=warn-with-error-code | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
dfabulich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const assert = require('assert'); | ||
|
||
common.disableCrashOnUnhandledRejection(); | ||
|
||
Promise.reject(new Error('alas')); | ||
dfabulich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
process.on('exit', assert.strictEqual.bind(null, 1)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
*UnhandledPromiseRejectionWarning: Error: alas | ||
at *promise_unhandled_warn_with_error.js:*:* | ||
at * | ||
at * | ||
at * | ||
at * | ||
at * | ||
at * | ||
(Use `node --trace-warnings ...` to show where the warning was created) | ||
*UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Flags: --unhandled-rejections=throw | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const Countdown = require('../common/countdown'); | ||
const assert = require('assert'); | ||
|
||
common.disableCrashOnUnhandledRejection(); | ||
|
||
// Verify that the unhandledRejection handler prevents triggering | ||
// uncaught exceptions | ||
|
||
const err1 = new Error('One'); | ||
|
||
const errors = [err1, null]; | ||
|
||
const ref = new Promise(() => { | ||
throw err1; | ||
}); | ||
// Explicitly reject `null`. | ||
Promise.reject(null); | ||
|
||
process.on('warning', common.mustNotCall('warning')); | ||
process.on('rejectionHandled', common.mustNotCall('rejectionHandled')); | ||
process.on('exit', assert.strictEqual.bind(null, 0)); | ||
process.on('uncaughtException', common.mustNotCall('uncaughtException')); | ||
|
||
const timer = setTimeout(() => console.log(ref), 1000); | ||
|
||
const counter = new Countdown(2, () => { | ||
clearTimeout(timer); | ||
}); | ||
|
||
process.on('unhandledRejection', common.mustCall((err) => { | ||
counter.dec(); | ||
const knownError = errors.shift(); | ||
assert.deepStrictEqual(err, knownError); | ||
}, 2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Flags: --unhandled-rejections=throw | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const Countdown = require('../common/countdown'); | ||
const assert = require('assert'); | ||
|
||
common.disableCrashOnUnhandledRejection(); | ||
|
||
// Verify that unhandled rejections always trigger uncaught exceptions instead | ||
// of triggering unhandled rejections. | ||
|
||
const err1 = new Error('One'); | ||
const err2 = new Error( | ||
'This error originated either by throwing ' + | ||
'inside of an async function without a catch block, or by rejecting a ' + | ||
'promise which was not handled with .catch(). The promise rejected with the' + | ||
' reason "null".' | ||
); | ||
err2.code = 'ERR_UNHANDLED_REJECTION'; | ||
Object.defineProperty(err2, 'name', { | ||
value: 'UnhandledPromiseRejection', | ||
writable: true, | ||
configurable: true | ||
}); | ||
|
||
const errors = [err1, err2]; | ||
const identical = [true, false]; | ||
|
||
const ref = new Promise(() => { | ||
throw err1; | ||
}); | ||
// Explicitly reject `null`. | ||
Promise.reject(null); | ||
|
||
process.on('warning', common.mustNotCall('warning')); | ||
// If we add an unhandledRejection handler, the exception won't be thrown | ||
// process.on('unhandledRejection', common.mustCall(2)); | ||
dfabulich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
process.on('rejectionHandled', common.mustNotCall('rejectionHandled')); | ||
process.on('exit', assert.strictEqual.bind(null, 0)); | ||
|
||
const timer = setTimeout(() => console.log(ref), 1000); | ||
|
||
const counter = new Countdown(2, () => { | ||
clearTimeout(timer); | ||
}); | ||
|
||
process.on('uncaughtException', common.mustCall((err, origin) => { | ||
counter.dec(); | ||
assert.strictEqual(origin, 'unhandledRejection', err); | ||
const knownError = errors.shift(); | ||
assert.deepStrictEqual(err, knownError); | ||
// Check if the errors are reference equal. | ||
assert(identical.shift() ? err === knownError : err !== knownError); | ||
}, 2)); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for bikeshedding the name further but what about:
warn-and-exit
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OK with a bit more bikeshedding on this, but be advised that I'd already filed a PR to update the name in the unhandled rejections survey. nodejs/TSC#872 It
IMO,
warn-and-exit
sounds like it will actually terminate the process, but this flag is designed to setprocess.exitCode = 1
and keep running. The exit code may never even take effect if it's a long-running process that never terminates until killed.I think
warn-with-error-code
is the shortest and clearest way to say, "trigger a warning, and set the process exit code to 1"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dfabulich I don't think it's a problem if we end up with different names on the survey, because we explain what each mode is there (and we're only using mode names there as an alias for what they mean).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my part, I can’t think of a better name for this than
warn-with-error-code
. Let’s merge this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ready to land. I'm very absent on GitHub due to everything that is happening, but another collaborator might land it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it help if this PR had the "author ready" label?