Skip to content

Commit

Permalink
repl: use object writer for thrown errors
Browse files Browse the repository at this point in the history
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 <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax authored and BridgeAR committed Mar 3, 2019
1 parent 6c38fcf commit a0778a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,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 @@ -432,7 +432,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

0 comments on commit a0778a9

Please sign in to comment.