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

process: Cast promise rejection reason to string #11640

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion lib/internal/process/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function setupPromises(scheduleMicrotasks) {

function emitWarning(uid, reason) {
const warning = new Error('Unhandled promise rejection ' +
`(rejection id: ${uid}): ${reason}`);
`(rejection id: ${uid}): ${String(reason)}`);
warning.name = 'UnhandledPromiseRejectionWarning';
warning.id = uid;
if (reason instanceof Error) {
Expand Down
42 changes: 35 additions & 7 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,17 +600,45 @@ exports.isAlive = function isAlive(pid) {
}
};

exports.expectWarning = function(name, expected) {
if (typeof expected === 'string')
expected = [expected];
process.on('warning', exports.mustCall((warning) => {
function expectWarning(name, expectedMessages) {
return exports.mustCall((warning) => {
assert.strictEqual(warning.name, name);
assert.ok(expected.includes(warning.message),
assert.ok(expectedMessages.includes(warning.message),
`unexpected error message: "${warning.message}"`);
// Remove a warning message after it is seen so that we guarantee that we
// get each message only once.
expected.splice(expected.indexOf(warning.message), 1);
}, expected.length));
expectedMessages.splice(expectedMessages.indexOf(warning.message), 1);
}, expectedMessages.length);
}

function expectWarningByName(name, expected) {
if (typeof expected === 'string') {
expected = [expected];
}
process.on('warning', expectWarning(name, expected));
}

function expectWarningByMap(warningMap) {
const catchWarning = {};
Object.keys(warningMap).forEach((name) => {
let expected = warningMap[name];
if (typeof expected === 'string') {
expected = [expected];
}
catchWarning[name] = expectWarning(name, expected);
});
process.on('warning', (warning) => catchWarning[warning.name](warning));
}

// accepts a warning name and description or array of descriptions or a map
// of warning names to description(s)
// ensures a warning is generated for each name/description pair
exports.expectWarning = function(nameOrMap, expected) {
if (typeof nameOrMap === 'string') {
expectWarningByName(nameOrMap, expected);
} else {
expectWarningByMap(nameOrMap);
}
};

Object.defineProperty(exports, 'hasIntl', {
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-promises-unhandled-symbol-rejections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
const common = require('../common');

const expectedDeprecationWarning = 'Unhandled promise rejections are ' +
'deprecated. In the future, promise ' +
'rejections that are not handled will ' +
'terminate the Node.js process with a ' +
'non-zero exit code.';
const expectedPromiseWarning = 'Unhandled promise rejection (rejection id: ' +
'1): Symbol()';

common.expectWarning({
DeprecationWarning: expectedDeprecationWarning,
UnhandledPromiseRejectionWarning: expectedPromiseWarning,
});

// ensure this doesn't crash
Promise.reject(Symbol());