Skip to content

Commit 030a028

Browse files
author
Benjamin Gruenbaum
committed
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 0e5283b commit 030a028

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
@@ -283,17 +283,23 @@ function REPLServer(prompt,
283283
self._domain.on('error', function debugDomainError(e) {
284284
debug('domain error');
285285
const top = replMap.get(self);
286+
286287
internalUtil.decorateErrorStack(e);
288+
const isError = internalUtil.isError(e);
287289
if (e instanceof SyntaxError && e.stack) {
288290
// remove repl:line-number and stack trace
289291
e.stack = e.stack
290292
.replace(/^repl:\d+\r?\n/, '')
291293
.replace(/^\s+at\s.*\n?/gm, '');
292-
} else if (e.stack && self.replMode === exports.REPL_MODE_STRICT) {
294+
} else if (isError && self.replMode === exports.REPL_MODE_STRICT) {
293295
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
294296
(_, pre, line) => pre + (line - 1));
295297
}
296-
top.outputStream.write((e.stack || e) + '\n');
298+
if (isError && e.stack) {
299+
top.outputStream.write(`${e.stack}\n`);
300+
} else {
301+
top.outputStream.write(`Thrown: ${String(e)}\n`);
302+
}
297303
top.bufferedCommand = '';
298304
top.lines.level = [];
299305
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)