From a4824874fb8e3f3802ebd2f7bbf5885d18f0f7e6 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 12 Apr 2021 11:42:54 +0200 Subject: [PATCH 1/2] 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 --- 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..6bd4a291176bce --- /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'); +} From 22504275a58814b37330de9de266e0cbd325bf1e Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 12 Apr 2021 12:13:37 +0200 Subject: [PATCH 2/2] fixup! repl: fix error message printing Co-authored-by: Antoine du Hamel --- test/parallel/test-repl-pretty-stack-custom-writer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-repl-pretty-stack-custom-writer.js b/test/parallel/test-repl-pretty-stack-custom-writer.js index 6bd4a291176bce..877f8cb8077597 100644 --- a/test/parallel/test-repl-pretty-stack-custom-writer.js +++ b/test/parallel/test-repl-pretty-stack-custom-writer.js @@ -17,7 +17,7 @@ const repl = require('repl'); useColors: false }); - r.write(`throw new Error("foo[a]")\n`); + r.write('throw new Error("foo[a]")\n'); r.close(); assert.strictEqual(output.read().toString(), 'Uncaught Error: foo[a]\n'); }