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 <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Yongsheng Zhang <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
addaleax authored and danielleadams committed Apr 29, 2021
1 parent a7da993 commit 8d4936d
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 @@ -610,7 +610,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 8d4936d

Please sign in to comment.