Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mimic node more closely #77

Merged
merged 1 commit into from
Jun 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,21 +355,35 @@ function getErrorSource(error) {
return null;
}

// Mimic node's stack trace printing when an exception escapes the process
function handleUncaughtExceptions(error) {
if (!error || !error.stack) {
console.error('Uncaught exception:', error);
} else {
var source = getErrorSource(error);
if (source !== null) {
console.error();
console.error(source);
}
console.error(error.stack);
function printErrorAndExit (error) {
var source = getErrorSource(error);

if (source) {
console.error();
console.error(source);
}

console.error(error.stack);
process.exit(1);
}

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

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]);
}
}

return origEmit.apply(this, arguments);
}
}

exports.wrapCallSite = wrapCallSite;
exports.getErrorSource = getErrorSource;
exports.mapSourcePosition = mapSourcePosition;
Expand Down Expand Up @@ -405,7 +419,7 @@ exports.install = function(options) {
// generated JavaScript code will be shown above the stack trace instead of
// the original source code.
if (installHandler && !isInBrowser()) {
process.on('uncaughtException', handleUncaughtExceptions);
shimEmitUncaughtException();
}
}
};
22 changes: 22 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,25 @@ it('finds source maps with charset specified', function() {
}
fs.unlinkSync('.generated.js');
});

it('handleUncaughtExceptions is true with existing listener', function(done) {
var source = [
'process.on("uncaughtException", function() { /* Silent */ });',
'function foo() { throw new Error("this is the error"); }',
'require("./source-map-support").install();',
'process.nextTick(foo);',
'//@ sourceMappingURL=.generated.js.map'
];

fs.writeFileSync('.original.js', 'this is the original code');
fs.writeFileSync('.generated.js.map', createSingleLineSourceMap());
fs.writeFileSync('.generated.js', source.join('\n'));

child_process.exec('node ./.generated', function(error, stdout, stderr) {
fs.unlinkSync('.generated.js');
fs.unlinkSync('.generated.js.map');
fs.unlinkSync('.original.js');
assert.equal((stdout + stderr).trim(), '');
done();
});
});