@@ -486,7 +486,7 @@ function getErrorSource(error) {
486486 return null ;
487487}
488488
489- function printErrorAndExit ( error ) {
489+ function printFatalErrorUponExit ( error ) {
490490 var source = getErrorSource ( error ) ;
491491
492492 // Ensure error is printed synchronously and not truncated
@@ -505,23 +505,24 @@ function printErrorAndExit (error) {
505505 colors : process . stderr . isTTY
506506 } )
507507 ) ;
508- process . exit ( 1 ) ;
509508}
510509
511510function shimEmitUncaughtException ( ) {
512511 var origEmit = process . emit ;
512+ var isTerminatingDueToFatalException = false ;
513+ var fatalException ;
513514
514515 process . emit = function ( type ) {
515- if ( type === 'uncaughtException' ) {
516- var hasStack = ( arguments [ 1 ] && arguments [ 1 ] . stack ) ;
517- var hasListeners = ( this . listeners ( type ) . length > 0 ) ;
518-
519- if ( hasStack && ! hasListeners ) {
520- return printErrorAndExit ( arguments [ 1 ] ) ;
521- }
516+ const hadListeners = origEmit . apply ( this , arguments ) ;
517+ if ( type === 'uncaughtException' && ! hadListeners ) {
518+ isTerminatingDueToFatalException = true ;
519+ fatalException = arguments [ 1 ] ;
520+ process . exit ( 1 ) ;
522521 }
523-
524- return origEmit . apply ( this , arguments ) ;
522+ if ( type === 'exit' && isTerminatingDueToFatalException ) {
523+ printFatalErrorUponExit ( fatalException ) ;
524+ }
525+ return hadListeners ;
525526 } ;
526527}
527528
0 commit comments