Skip to content

Commit 4820368

Browse files
authored
Merge pull request #16 from cspotcode/ab/fix-exit-logging-order
Fix #15: process 'exit' listeners should run before fatal error is logged
2 parents e9c6c84 + df10eae commit 4820368

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

source-map-support.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

511510
function 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

Comments
 (0)