Skip to content

Commit

Permalink
repl: fix error message printing
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
addaleax authored and richardlau committed Dec 14, 2021
1 parent f0be077 commit 70e094a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-repl-pretty-stack-custom-writer.js
Original file line number Diff line number Diff line change
@@ -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');
}

0 comments on commit 70e094a

Please sign in to comment.