Skip to content

Commit 70e094a

Browse files
addaleaxrichardlau
authored andcommittedDec 14, 2021
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: #22436 PR-URL: #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>
1 parent f0be077 commit 70e094a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed
 

Diff for: ‎lib/repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ function REPLServer(prompt,
566566
errStack = self.writer(e);
567567

568568
// Remove one line error braces to keep the old style in place.
569-
if (errStack[errStack.length - 1] === ']') {
569+
if (errStack[0] === '[' && errStack[errStack.length - 1] === ']') {
570570
errStack = errStack.slice(1, -1);
571571
}
572572
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
require('../common');
3+
const { PassThrough } = require('stream');
4+
const assert = require('assert');
5+
const repl = require('repl');
6+
7+
{
8+
const input = new PassThrough();
9+
const output = new PassThrough();
10+
11+
const r = repl.start({
12+
prompt: '',
13+
input,
14+
output,
15+
writer: String,
16+
terminal: false,
17+
useColors: false
18+
});
19+
20+
r.write('throw new Error("foo[a]")\n');
21+
r.close();
22+
assert.strictEqual(output.read().toString(), 'Uncaught Error: foo[a]\n');
23+
}

0 commit comments

Comments
 (0)
Please sign in to comment.