From 4ea4e72870f794044b7c98fd8aa0e8f829ca4fc7 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 12 Apr 2021 11:42:54 +0200 Subject: [PATCH] repl: fix error message printing The REPL implementation would strip away the first and last character of a formatted error message if it ended with `]` (but with the obviously missing check for a starting `]`), leading to things like `Uncaught rror: foo[a` being printed for input like `Error: foo[a]`. Refs: https://github.com/nodejs/node/pull/22436 PR-URL: https://github.com/nodejs/node/pull/38209 Reviewed-By: Antoine du Hamel Reviewed-By: Rich Trott Reviewed-By: Yongsheng Zhang Reviewed-By: Colin Ihrig Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell Reviewed-By: Luigi Pinca --- lib/repl.js | 2 +- .../test-repl-pretty-stack-custom-writer.js | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-repl-pretty-stack-custom-writer.js diff --git a/lib/repl.js b/lib/repl.js index 4787107929d949..cf408516c6fce1 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -651,7 +651,7 @@ function REPLServer(prompt, errStack = self.writer(e); // Remove one line error braces to keep the old style in place. - if (errStack[errStack.length - 1] === ']') { + if (errStack[0] === '[' && errStack[errStack.length - 1] === ']') { errStack = StringPrototypeSlice(errStack, 1, -1); } } diff --git a/test/parallel/test-repl-pretty-stack-custom-writer.js b/test/parallel/test-repl-pretty-stack-custom-writer.js new file mode 100644 index 00000000000000..877f8cb8077597 --- /dev/null +++ b/test/parallel/test-repl-pretty-stack-custom-writer.js @@ -0,0 +1,23 @@ +'use strict'; +require('../common'); +const { PassThrough } = require('stream'); +const assert = require('assert'); +const repl = require('repl'); + +{ + const input = new PassThrough(); + const output = new PassThrough(); + + const r = repl.start({ + prompt: '', + input, + output, + writer: String, + terminal: false, + useColors: false + }); + + r.write('throw new Error("foo[a]")\n'); + r.close(); + assert.strictEqual(output.read().toString(), 'Uncaught Error: foo[a]\n'); +}