55 ArrayPrototypeShift,
66 Error,
77 ObjectDefineProperty,
8+ ObjectPrototypeHasOwnProperty,
89 SafeWeakMap,
910} = primordials ;
1011
@@ -79,6 +80,12 @@ function hasRejectionToWarn() {
7980 return tickInfo [ kHasRejectionToWarn ] === 1 ;
8081}
8182
83+ function isErrorLike ( o ) {
84+ return typeof o === 'object' &&
85+ o !== null &&
86+ ObjectPrototypeHasOwnProperty ( o , 'stack' ) ;
87+ }
88+
8289function getUnhandledRejectionsMode ( ) {
8390 const { getOptionValue } = require ( 'internal/options' ) ;
8491 switch ( getOptionValue ( '--unhandled-rejections' ) ) {
@@ -179,14 +186,21 @@ function emitUnhandledRejectionWarning(uid, reason) {
179186 `(rejection id: ${ uid } )`
180187 ) ;
181188 try {
182- if ( reason instanceof Error ) {
189+ if ( isErrorLike ( reason ) ) {
183190 warning . stack = reason . stack ;
184191 process . emitWarning ( reason . stack , unhandledRejectionErrName ) ;
185192 } else {
186193 process . emitWarning (
187194 noSideEffectsToString ( reason ) , unhandledRejectionErrName ) ;
188195 }
189- } catch { }
196+ } catch {
197+ try {
198+ process . emitWarning (
199+ noSideEffectsToString ( reason ) , unhandledRejectionErrName ) ;
200+ } catch {
201+ // Ignore.
202+ }
203+ }
190204
191205 process . emitWarning ( warning ) ;
192206}
@@ -232,7 +246,7 @@ function processPromiseRejections() {
232246 try {
233247 switch ( unhandledRejectionsMode ) {
234248 case kStrictUnhandledRejections : {
235- const err = reason instanceof Error ?
249+ const err = isErrorLike ( reason ) ?
236250 reason : generateUnhandledRejectionError ( reason ) ;
237251 // This destroys the async stack, don't clear it after
238252 triggerUncaughtException ( err , true /* fromPromise */ ) ;
@@ -259,7 +273,7 @@ function processPromiseRejections() {
259273 case kThrowUnhandledRejections : {
260274 const handled = emit ( reason , promise , promiseInfo ) ;
261275 if ( ! handled ) {
262- const err = reason instanceof Error ?
276+ const err = isErrorLike ( reason ) ?
263277 reason : generateUnhandledRejectionError ( reason ) ;
264278 // This destroys the async stack, don't clear it after
265279 triggerUncaughtException ( err , true /* fromPromise */ ) ;
0 commit comments