Skip to content

Commit d557e58

Browse files
committed
mimic node more closely
1 parent e0842b9 commit d557e58

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

source-map-support.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -355,21 +355,35 @@ function getErrorSource(error) {
355355
return null;
356356
}
357357

358-
// Mimic node's stack trace printing when an exception escapes the process
359-
function handleUncaughtExceptions(error) {
360-
if (!error || !error.stack) {
361-
console.error('Uncaught exception:', error);
362-
} else {
363-
var source = getErrorSource(error);
364-
if (source !== null) {
365-
console.error();
366-
console.error(source);
367-
}
368-
console.error(error.stack);
358+
function printErrorAndExit (error) {
359+
var source = getErrorSource(error);
360+
361+
if (source) {
362+
console.error();
363+
console.error(source);
369364
}
365+
366+
console.error(error.stack);
370367
process.exit(1);
371368
}
372369

370+
function shimEmitUncaughtException () {
371+
var origEmit = process.emit;
372+
373+
process.emit = function (type) {
374+
if (type === 'uncaughtException') {
375+
var hasStack = (arguments[1] && arguments[1].stack);
376+
var hasListeners = (this.listeners(type).length > 0);
377+
378+
if (hasStack && !hasListeners) {
379+
return printErrorAndExit(arguments[1]);
380+
}
381+
}
382+
383+
return origEmit.apply(this, arguments);
384+
}
385+
}
386+
373387
exports.wrapCallSite = wrapCallSite;
374388
exports.getErrorSource = getErrorSource;
375389
exports.mapSourcePosition = mapSourcePosition;
@@ -405,7 +419,7 @@ exports.install = function(options) {
405419
// generated JavaScript code will be shown above the stack trace instead of
406420
// the original source code.
407421
if (installHandler && !isInBrowser()) {
408-
process.on('uncaughtException', handleUncaughtExceptions);
422+
shimEmitUncaughtException();
409423
}
410424
}
411425
};

test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,25 @@ it('finds source maps with charset specified', function() {
415415
}
416416
fs.unlinkSync('.generated.js');
417417
});
418+
419+
it('handleUncaughtExceptions is true with existing listener', function(done) {
420+
var source = [
421+
'process.on("uncaughtException", function() { /* Silent */ });',
422+
'function foo() { throw new Error("this is the error"); }',
423+
'require("./source-map-support").install();',
424+
'process.nextTick(foo);',
425+
'//@ sourceMappingURL=.generated.js.map'
426+
];
427+
428+
fs.writeFileSync('.original.js', 'this is the original code');
429+
fs.writeFileSync('.generated.js.map', createSingleLineSourceMap());
430+
fs.writeFileSync('.generated.js', source.join('\n'));
431+
432+
child_process.exec('node ./.generated', function(error, stdout, stderr) {
433+
fs.unlinkSync('.generated.js');
434+
fs.unlinkSync('.generated.js.map');
435+
fs.unlinkSync('.original.js');
436+
assert.equal((stdout + stderr).trim(), '');
437+
done();
438+
});
439+
});

0 commit comments

Comments
 (0)