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>
This commit is contained in:
Anna Henningsen authored and James M Snell committed 2021-04-13 09:48:06 -07:00
1 parent 23d2c54bab
commit e7391967c2
2 files changed
+24 -1

No files matched your search

+1 -1
View File
@@ -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);
}
}
@@ -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');
}