Skip to content

Commit 34821f6

Browse files
Benjamin GruenbaumFishrock123
Benjamin Gruenbaum
authored andcommitted
repl: don't terminate on null thrown
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 <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Colin Ihrig <[email protected]
1 parent 53ad91c commit 34821f6

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/repl.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,23 @@ function REPLServer(prompt,
282282
self._domain.on('error', function debugDomainError(e) {
283283
debug('domain error');
284284
const top = replMap.get(self);
285+
285286
internalUtil.decorateErrorStack(e);
287+
const isError = internalUtil.isError(e);
286288
if (e instanceof SyntaxError && e.stack) {
287289
// remove repl:line-number and stack trace
288290
e.stack = e.stack
289291
.replace(/^repl:\d+\r?\n/, '')
290292
.replace(/^\s+at\s.*\n?/gm, '');
291-
} else if (e.stack && self.replMode === exports.REPL_MODE_STRICT) {
293+
} else if (isError && self.replMode === exports.REPL_MODE_STRICT) {
292294
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
293295
(_, pre, line) => pre + (line - 1));
294296
}
295-
top.outputStream.write((e.stack || e) + '\n');
297+
if (isError && e.stack) {
298+
top.outputStream.write(`${e.stack}\n`);
299+
} else {
300+
top.outputStream.write(`Thrown: ${String(e)}\n`);
301+
}
296302
top.bufferedCommand = '';
297303
top.lines.level = [];
298304
top.displayPrompt();
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
require('../common');
3+
const repl = require('repl');
4+
const assert = require('assert');
5+
const Stream = require('stream');
6+
7+
const output = new Stream();
8+
let text = '';
9+
output.write = output.pause = output.resume = function(buf) {
10+
text += buf.toString();
11+
};
12+
13+
const replserver = repl.start({
14+
output: output,
15+
input: process.stdin
16+
});
17+
18+
replserver.emit('line', 'process.nextTick(() => { throw null; })');
19+
replserver.emit('line', '.exit');
20+
21+
setTimeout(() => {
22+
console.log(text);
23+
assert(text.includes('Thrown: null'));
24+
}, 0);

0 commit comments

Comments
 (0)