Skip to content

Commit

Permalink
Merge pull request #16 from cspotcode/ab/fix-exit-logging-order
Browse files Browse the repository at this point in the history
Fix #15: process 'exit' listeners should run before fatal error is logged
  • Loading branch information
cspotcode authored Jul 21, 2021
2 parents e9c6c84 + df10eae commit 4820368
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ function getErrorSource(error) {
return null;
}

function printErrorAndExit (error) {
function printFatalErrorUponExit (error) {
var source = getErrorSource(error);

// Ensure error is printed synchronously and not truncated
Expand All @@ -505,23 +505,24 @@ function printErrorAndExit (error) {
colors: process.stderr.isTTY
})
);
process.exit(1);
}

function shimEmitUncaughtException () {
var origEmit = process.emit;
var isTerminatingDueToFatalException = false;
var fatalException;

process.emit = function (type) {
if (type === 'uncaughtException') {
var hasStack = (arguments[1] && arguments[1].stack);
var hasListeners = (this.listeners(type).length > 0);

if (hasStack && !hasListeners) {
return printErrorAndExit(arguments[1]);
}
const hadListeners = origEmit.apply(this, arguments);
if (type === 'uncaughtException' && !hadListeners) {
isTerminatingDueToFatalException = true;
fatalException = arguments[1];
process.exit(1);
}

return origEmit.apply(this, arguments);
if (type === 'exit' && isTerminatingDueToFatalException) {
printFatalErrorUponExit(fatalException);
}
return hadListeners;
};
}

Expand Down

0 comments on commit 4820368

Please sign in to comment.