From a29fe914f116b290c85927777740663043014b9a Mon Sep 17 00:00:00 2001 From: Madara Uchiha Date: Mon, 20 Nov 2017 21:22:37 +0200 Subject: [PATCH] Improved unhandled rejection message Fixes #16768 --- lib/internal/process/promises.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/internal/process/promises.js b/lib/internal/process/promises.js index 663e6f5fec6985..7710bfaf5f118f 100644 --- a/lib/internal/process/promises.js +++ b/lib/internal/process/promises.js @@ -1,7 +1,5 @@ 'use strict'; -const { safeToString } = process.binding('util'); - const promiseRejectEvent = process._promiseRejectEvent; const hasBeenNotifiedProperty = new WeakMap(); const promiseToGuidProperty = new WeakMap(); @@ -60,11 +58,23 @@ function setupPromises(scheduleMicrotasks) { } function emitWarning(uid, reason) { + try { + if (reason instanceof Error) { + process.emitWarning(reason.stack, 'UnhandledPromiseRejectionWarning'); + } else { + process.emitWarning(reason, 'UnhandledPromiseRejectionWarning'); + } + } catch (e) { + // ignored + } + const warning = new Error( - `Unhandled promise rejection (rejection id: ${uid}): ` + - safeToString(reason)); + '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(). ' + + `(rejection id: ${uid})` + ); warning.name = 'UnhandledPromiseRejectionWarning'; - warning.id = uid; try { if (reason instanceof Error) { warning.stack = reason.stack;