Skip to content

Commit 4c254d6

Browse files
addaleaxBridgeAR
authored andcommitted
repl: use object writer for thrown errors
This makes us use the defaults that were set for the REPL, i.e. aligns with the printing of expression completion values, and in particular enables color support. PR-URL: #26361 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anto Aravinth <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 93d7fa3 commit 4c254d6

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lib/repl.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ function REPLServer(prompt,
416416
(_, pre, line) => pre + (line - 1));
417417
}
418418
}
419-
errStack = util.inspect(e);
419+
errStack = self.writer(e);
420420

421421
// Remove one line error braces to keep the old style in place.
422422
if (errStack[errStack.length - 1] === ']') {
@@ -426,7 +426,7 @@ function REPLServer(prompt,
426426
}
427427

428428
if (errStack === '') {
429-
errStack = `Thrown: ${util.inspect(e)}\n`;
429+
errStack = `Thrown: ${self.writer(e)}\n`;
430430
} else {
431431
const ln = errStack.endsWith('\n') ? '' : '\n';
432432
errStack = `Thrown:\n${errStack}${ln}`;

test/parallel/test-repl-pretty-stack.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const assert = require('assert');
66
const repl = require('repl');
77

88

9-
function run({ command, expected }) {
9+
function run({ command, expected, ...extraREPLOptions }) {
1010
let accum = '';
1111

1212
const inputStream = new ArrayStream();
@@ -19,7 +19,8 @@ function run({ command, expected }) {
1919
input: inputStream,
2020
output: outputStream,
2121
terminal: false,
22-
useColors: false
22+
useColors: false,
23+
...extraREPLOptions
2324
});
2425

2526
r.write(`${command}\n`);
@@ -44,6 +45,18 @@ const tests = [
4445
command: 'throw new Error(\'Whoops!\')',
4546
expected: 'Thrown:\nError: Whoops!\n'
4647
},
48+
{
49+
command: '(() => { const err = Error(\'Whoops!\'); ' +
50+
'err.foo = \'bar\'; throw err; })()',
51+
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22 foo: \'bar\' }\n',
52+
},
53+
{
54+
command: '(() => { const err = Error(\'Whoops!\'); ' +
55+
'err.foo = \'bar\'; throw err; })()',
56+
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22 foo: ' +
57+
"\u001b[32m'bar'\u001b[39m }\n",
58+
useColors: true
59+
},
4760
{
4861
command: 'foo = bar;',
4962
expected: 'Thrown:\nReferenceError: bar is not defined\n'

0 commit comments

Comments
 (0)