summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2021-04-12 11:42:54 +0200
committerRichard Lau <rlau@redhat.com>2021-12-14 15:32:15 +0000
commit70e094a26b546e1e372d67570cd7f03109bc03d5 (patch)
tree755219cb4c1428deb21963b7cd082203b6a57239
parentf0be07796ebdde01ff3b0fb580336d566c9f69a0 (diff)
downloadnode-new-70e094a26b546e1e372d67570cd7f03109bc03d5.tar.gz
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 <duhamelantoine1995@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
-rw-r--r--lib/repl.js2
-rw-r--r--test/parallel/test-repl-pretty-stack-custom-writer.js23
2 files changed, 24 insertions, 1 deletions
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');
+}