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 authored and Fishrock123 committed Jul 24, 2017
1 parent 53ad91c commit 34821f6
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
Expand Up @@ -282,17 +282,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
@@ -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 34821f6

Please sign in to comment.