From a46e63b717f6bed94fa570f2f518b70bdab00123 Mon Sep 17 00:00:00 2001 From: Cameron Little Date: Mon, 13 Mar 2017 09:07:06 -0700 Subject: [PATCH] process: cast promise rejection reason to string The unhandled promise rejection warning uses a template literal and prints the reason a promise was rejected. If rejecting with a symbol, the symbol failed to convert to a string and the process crashed. Now, symbols are casted to strings and the process does not crash. Fixes: https://github.com/nodejs/node/issues/11637 PR-URL: https://github.com/nodejs/node/pull/11640 Reviewed-By: Anna Henningsen --- lib/internal/process/promises.js | 2 +- ...est-promises-unhandled-symbol-rejections.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-promises-unhandled-symbol-rejections.js diff --git a/lib/internal/process/promises.js b/lib/internal/process/promises.js index 83dee491a55c90..0fce2901b7408f 100644 --- a/lib/internal/process/promises.js +++ b/lib/internal/process/promises.js @@ -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) { diff --git a/test/parallel/test-promises-unhandled-symbol-rejections.js b/test/parallel/test-promises-unhandled-symbol-rejections.js new file mode 100644 index 00000000000000..a60a5f2e8aac30 --- /dev/null +++ b/test/parallel/test-promises-unhandled-symbol-rejections.js @@ -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());