From 70e094a26b546e1e372d67570cd7f03109bc03d5 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 12 Apr 2021 11:42:54 +0200 Subject: 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 10ca72f0e7..365df8a1bc 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -566,7 +566,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 = errStack.slice(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 0000000000..877f8cb807 --- /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'); +} -- cgit v1.2.1