From 4dfe7fdc9d8a052c1370d5b2b6bb99516a5b6ab2 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Sun, 16 Jul 2017 18:12:57 +0300 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/14306 Fixes: https://github.com/nodejs/node/issues/12373 Reviewed-By: Anna Henningsen --- lib/repl.js | 10 ++++++++-- test/parallel/test-repl-null-thrown.js | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-repl-null-thrown.js diff --git a/lib/repl.js b/lib/repl.js index 9954a300d70641..4efe02e94e9c44 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -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(); diff --git a/test/parallel/test-repl-null-thrown.js b/test/parallel/test-repl-null-thrown.js new file mode 100644 index 00000000000000..6a6473f9c35bc6 --- /dev/null +++ b/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('Error: null')); +}, 0);