5
5
ArrayPrototypeShift,
6
6
Error,
7
7
ObjectDefineProperty,
8
+ ObjectPrototypeHasOwnProperty,
8
9
SafeWeakMap,
9
10
} = primordials ;
10
11
@@ -79,6 +80,12 @@ function hasRejectionToWarn() {
79
80
return tickInfo [ kHasRejectionToWarn ] === 1 ;
80
81
}
81
82
83
+ function isErrorLike ( o ) {
84
+ return typeof o === 'object' &&
85
+ o !== null &&
86
+ ObjectPrototypeHasOwnProperty ( o , 'stack' ) ;
87
+ }
88
+
82
89
function getUnhandledRejectionsMode ( ) {
83
90
const { getOptionValue } = require ( 'internal/options' ) ;
84
91
switch ( getOptionValue ( '--unhandled-rejections' ) ) {
@@ -179,14 +186,21 @@ function emitUnhandledRejectionWarning(uid, reason) {
179
186
`(rejection id: ${ uid } )`
180
187
) ;
181
188
try {
182
- if ( reason instanceof Error ) {
189
+ if ( isErrorLike ( reason ) ) {
183
190
warning . stack = reason . stack ;
184
191
process . emitWarning ( reason . stack , unhandledRejectionErrName ) ;
185
192
} else {
186
193
process . emitWarning (
187
194
noSideEffectsToString ( reason ) , unhandledRejectionErrName ) ;
188
195
}
189
- } catch { }
196
+ } catch {
197
+ try {
198
+ process . emitWarning (
199
+ noSideEffectsToString ( reason ) , unhandledRejectionErrName ) ;
200
+ } catch {
201
+ // Ignore.
202
+ }
203
+ }
190
204
191
205
process . emitWarning ( warning ) ;
192
206
}
@@ -232,7 +246,7 @@ function processPromiseRejections() {
232
246
try {
233
247
switch ( unhandledRejectionsMode ) {
234
248
case kStrictUnhandledRejections : {
235
- const err = reason instanceof Error ?
249
+ const err = isErrorLike ( reason ) ?
236
250
reason : generateUnhandledRejectionError ( reason ) ;
237
251
// This destroys the async stack, don't clear it after
238
252
triggerUncaughtException ( err , true /* fromPromise */ ) ;
@@ -259,7 +273,7 @@ function processPromiseRejections() {
259
273
case kThrowUnhandledRejections : {
260
274
const handled = emit ( reason , promise , promiseInfo ) ;
261
275
if ( ! handled ) {
262
- const err = reason instanceof Error ?
276
+ const err = isErrorLike ( reason ) ?
263
277
reason : generateUnhandledRejectionError ( reason ) ;
264
278
// This destroys the async stack, don't clear it after
265
279
triggerUncaughtException ( err , true /* fromPromise */ ) ;
0 commit comments