Skip to content

Commit

Permalink
repl: don't terminate on null thrown
Browse files Browse the repository at this point in the history
Previous behavior was to assume an error is a proper error in the
repl module. A check was added to not terminate the process on thrown
repl errors that are `null` or `undefined`.

PR-URL: #14306
Fixes: #12373
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com
  • Loading branch information
Benjamin Gruenbaum committed Jul 19, 2017
1 parent 0e5283b commit 030a028
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,23 @@ function REPLServer(prompt,
self._domain.on('error', function debugDomainError(e) {
debug('domain error');
const top = replMap.get(self);

internalUtil.decorateErrorStack(e);
const isError = internalUtil.isError(e);
if (e instanceof SyntaxError && e.stack) {
// remove repl:line-number and stack trace
e.stack = e.stack
.replace(/^repl:\d+\r?\n/, '')
.replace(/^\s+at\s.*\n?/gm, '');
} else if (e.stack && self.replMode === exports.REPL_MODE_STRICT) {
} else if (isError && self.replMode === exports.REPL_MODE_STRICT) {
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
(_, pre, line) => pre + (line - 1));
}
top.outputStream.write((e.stack || e) + '\n');
if (isError && e.stack) {
top.outputStream.write(`${e.stack}\n`);
} else {
top.outputStream.write(`Thrown: ${String(e)}\n`);
}
top.bufferedCommand = '';
top.lines.level = [];
top.displayPrompt();
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-repl-null-thrown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
require('../common');
const repl = require('repl');
const assert = require('assert');
const Stream = require('stream');

const output = new Stream();
let text = '';
output.write = output.pause = output.resume = function(buf) {
text += buf.toString();
};

const replserver = repl.start({
output: output,
input: process.stdin
});

replserver.emit('line', 'process.nextTick(() => { throw null; })');
replserver.emit('line', '.exit');

setTimeout(() => {
console.log(text);
assert(text.includes('Thrown: null'));
}, 0);

0 comments on commit 030a028

Please sign in to comment.