@@ -335,49 +335,49 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
335
335
} else if ( ! doError )
336
336
return false ;
337
337
338
- const domain = this . domain ;
339
-
340
338
// If there is no 'error' event listener then throw.
341
339
if ( doError ) {
342
340
let er ;
343
341
if ( args . length > 0 )
344
342
er = args [ 0 ] ;
345
- if ( domain ) {
343
+ if ( ! this . domain ) {
344
+ if ( er instanceof Error ) {
345
+ try {
346
+ const capture = { } ;
347
+ ErrorCaptureStackTrace ( capture , EventEmitter . prototype . emit ) ;
348
+ ObjectDefineProperty ( er , kEnhanceStackBeforeInspector , {
349
+ value : enhanceStackTrace . bind ( this , er , capture ) ,
350
+ configurable : true
351
+ } ) ;
352
+ } catch { }
353
+
354
+ // Note: The comments on the `throw` lines are intentional, they show
355
+ // up in Node's output if this results in an unhandled exception.
356
+ throw er ; // Unhandled 'error' event
357
+ } else {
358
+ let stringifiedEr ;
359
+ const { inspect } = require ( 'internal/util/inspect' ) ;
360
+ try {
361
+ stringifiedEr = inspect ( er ) ;
362
+ } catch {
363
+ stringifiedEr = er ;
364
+ }
365
+
366
+ // At least give some kind of context to the user
367
+ const err = new ERR_UNHANDLED_ERROR ( stringifiedEr ) ;
368
+ err . context = er ;
369
+ throw err ; // Unhandled 'error' event
370
+ }
371
+ } else {
346
372
if ( ! er ) {
347
373
er = new ERR_UNHANDLED_ERROR ( ) ;
348
374
}
349
375
if ( typeof er === 'object' ) {
350
376
er . domainEmitter = this ;
351
- er . domain = domain ;
377
+ er . domain = this . domain ;
352
378
er . domainThrown = false ;
353
379
}
354
- domain . emit ( 'error' , er ) ;
355
- } else if ( er instanceof Error ) {
356
- try {
357
- const capture = { } ;
358
- ErrorCaptureStackTrace ( capture , EventEmitter . prototype . emit ) ;
359
- ObjectDefineProperty ( er , kEnhanceStackBeforeInspector , {
360
- value : enhanceStackTrace . bind ( this , er , capture ) ,
361
- configurable : true
362
- } ) ;
363
- } catch { }
364
-
365
- // Note: The comments on the `throw` lines are intentional, they show
366
- // up in Node's output if this results in an unhandled exception.
367
- throw er ; // Unhandled 'error' event
368
- } else {
369
- let stringifiedEr ;
370
- const { inspect } = require ( 'internal/util/inspect' ) ;
371
- try {
372
- stringifiedEr = inspect ( er ) ;
373
- } catch {
374
- stringifiedEr = er ;
375
- }
376
-
377
- // At least give some kind of context to the user
378
- const err = new ERR_UNHANDLED_ERROR ( stringifiedEr ) ;
379
- err . context = er ;
380
- throw err ; // Unhandled 'error' event
380
+ this . domain . emit ( 'error' , er ) ;
381
381
}
382
382
383
383
return false ;
@@ -389,8 +389,8 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
389
389
return false ;
390
390
391
391
let needDomainExit = false ;
392
- if ( domain && this !== process ) {
393
- domain . enter ( ) ;
392
+ if ( this . domain && this !== process ) {
393
+ this . domain . enter ( ) ;
394
394
needDomainExit = true ;
395
395
}
396
396
@@ -421,7 +421,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
421
421
}
422
422
423
423
if ( needDomainExit )
424
- domain . exit ( ) ;
424
+ this . domain . exit ( ) ;
425
425
426
426
return true ;
427
427
} ;
0 commit comments