Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: use object writer for thrown errors #26361

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ function REPLServer(prompt,
(_, pre, line) => pre + (line - 1));
}
}
errStack = util.inspect(e);
errStack = self.writer(e);

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

if (errStack === '') {
errStack = `Thrown: ${util.inspect(e)}\n`;
errStack = `Thrown: ${self.writer(e)}\n`;
} else {
const ln = errStack.endsWith('\n') ? '' : '\n';
errStack = `Thrown:\n${errStack}${ln}`;
Expand Down
17 changes: 15 additions & 2 deletions test/parallel/test-repl-pretty-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const assert = require('assert');
const repl = require('repl');


function run({ command, expected }) {
function run({ command, expected, ...extraREPLOptions }) {
let accum = '';

const inputStream = new ArrayStream();
Expand All @@ -19,7 +19,8 @@ function run({ command, expected }) {
input: inputStream,
output: outputStream,
terminal: false,
useColors: false
useColors: false,
...extraREPLOptions
});

r.write(`${command}\n`);
Expand All @@ -44,6 +45,18 @@ const tests = [
command: 'throw new Error(\'Whoops!\')',
expected: 'Thrown:\nError: Whoops!\n'
},
{
command: '(() => { const err = Error(\'Whoops!\'); ' +
'err.foo = \'bar\'; throw err; })()',
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22 foo: \'bar\' }\n',
},
{
command: '(() => { const err = Error(\'Whoops!\'); ' +
'err.foo = \'bar\'; throw err; })()',
expected: 'Thrown:\n{ Error: Whoops!\n at repl:1:22 foo: ' +
"\u001b[32m'bar'\u001b[39m }\n",
useColors: true
},
{
command: 'foo = bar;',
expected: 'Thrown:\nReferenceError: bar is not defined\n'
Expand Down