Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
repl: add colors for uncaught exceptions and use a better indicator
This switches "Thrown:" with "Uncaught" to outline clearer that the
thrown error is not caught. It will also mark the error in red in
case the terminal supports colors / if colors are activated for the
repl.
  • Loading branch information
BridgeAR committed Nov 26, 2019
commit 1a7de3404c1c5f3ac11edb6a543b7ef5a3874eed
4 changes: 3 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ ObjectDefineProperty(inspect, 'defaultOptions', {
}
});

// TODO(BridgeAR): Add further color codes.
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
inspect.colors = ObjectAssign(ObjectCreate(null), {
bold: [1, 22],
Expand All @@ -271,7 +272,8 @@ inspect.colors = ObjectAssign(ObjectCreate(null), {
green: [32, 39],
magenta: [35, 39],
red: [31, 39],
yellow: [33, 39]
yellow: [33, 39],
brightRed: [91, 39]
});

// Don't use 'blue' not visible on cmd.exe
Expand Down
36 changes: 32 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,10 +547,38 @@ function REPLServer(prompt,
});
} else {
if (errStack === '') {
errStack = `Thrown: ${self.writer(e)}\n`;
} else {
const ln = errStack.endsWith('\n') ? '' : '\n';
errStack = `Thrown:\n${errStack}${ln}`;
errStack = self.writer(e);
}
const lines = errStack.split(/(?<=\n)/);
let colors = '';
if (options.useColors) {
colors = `\u001b[${inspect.colors.brightRed[0]}m` +
`\u001b[${inspect.colors.bold[0]}m`;
}
let matched = false;

errStack = colors;
for (const line of lines) {
if (!matched && /^\[?([A-Z][a-z0-9_]*)*Error/.test(line)) {
errStack += writer.options.breakLength >= line.length ?
`Uncaught ${line}` :
`Uncaught:\n${line}`;
matched = true;
} else {
errStack += line;
}
}
if (!matched) {
const ln = lines.length === 1 ? ' ' : ':\n';
// eslint-disable-next-line no-control-regex
errStack = errStack.replace(/\u001b\[[0-9]{1,3}m/g, '');
errStack = `${colors}Uncaught${ln}${errStack}`;
}
// Normalize line endings.
errStack += errStack.endsWith('\n') ? '' : '\n';
if (options.useColors) {
errStack += `\u001b[${inspect.colors.brightRed[1]}m` +
`\u001b[${inspect.colors.bold[1]}m`;
}
top.outputStream.write(errStack);
top.clearBufferedCommand();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-harmony.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const child = spawn(process.execPath, args);
const input = '(function(){"use strict"; const y=1;y=2})()\n';
// This message will vary based on JavaScript engine, so don't check the message
// contents beyond confirming that the `Error` is a `TypeError`.
const expectOut = /> Thrown:\nTypeError: /;
const expectOut = /> Uncaught TypeError: /;

child.stderr.setEncoding('utf8');
child.stderr.on('data', (d) => {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl-null-thrown.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ replserver.emit('line', '.exit');

setTimeout(() => {
console.log(text);
assert(text.includes('Thrown: null'));
assert(text.includes('Uncaught null'));
}, 0);
12 changes: 6 additions & 6 deletions test/parallel/test-repl-pretty-custom-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,26 @@ const tests = [
{
// test .load for a file that throws
command: `.load ${fixtures.path('repl-pretty-stack.js')}`,
expected: 'Thrown:\nError: Whoops!--->\nrepl:*:*--->\nd (repl:*:*)' +
expected: 'Uncaught Error: Whoops!--->\nrepl:*:*--->\nd (repl:*:*)' +
'--->\nc (repl:*:*)--->\nb (repl:*:*)--->\na (repl:*:*)\n'
},
{
command: 'let x y;',
expected: 'Thrown:\n' +
'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n'
expected: 'let x y;\n ^\n\n' +
'Uncaught SyntaxError: Unexpected identifier\n'
},
{
command: 'throw new Error(\'Whoops!\')',
expected: 'Thrown:\nError: Whoops!\n'
expected: 'Uncaught Error: Whoops!\n'
},
{
command: 'foo = bar;',
expected: 'Thrown:\nReferenceError: bar is not defined\n'
expected: 'Uncaught ReferenceError: bar is not defined\n'
},
// test anonymous IIFE
{
command: '(function() { throw new Error(\'Whoops!\'); })()',
expected: 'Thrown:\nError: Whoops!--->\nrepl:*:*\n'
expected: 'Uncaught Error: Whoops!--->\nrepl:*:*\n'
}
];

Expand Down
22 changes: 12 additions & 10 deletions test/parallel/test-repl-pretty-stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const repl = require('repl');

const stackRegExp = /(at .*repl:)[0-9]+:[0-9]+/g;

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

const inputStream = new ArrayStream();
Expand All @@ -25,6 +25,7 @@ function run({ command, expected, ...extraREPLOptions }) {
});

r.write(`${command}\n`);
console.log(i);
assert.strictEqual(
accum.replace(stackRegExp, '$1*:*'),
expected.replace(stackRegExp, '$1*:*')
Expand All @@ -36,39 +37,40 @@ const tests = [
{
// Test .load for a file that throws.
command: `.load ${fixtures.path('repl-pretty-stack.js')}`,
expected: 'Thrown:\nError: Whoops!\n at repl:*:*\n' +
expected: 'Uncaught Error: Whoops!\n at repl:*:*\n' +
' at d (repl:*:*)\n at c (repl:*:*)\n' +
' at b (repl:*:*)\n at a (repl:*:*)\n'
},
{
command: 'let x y;',
expected: 'Thrown:\n' +
'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n'
expected: 'let x y;\n ^\n\n' +
'Uncaught SyntaxError: Unexpected identifier\n'
},
{
command: 'throw new Error(\'Whoops!\')',
expected: 'Thrown:\nError: Whoops!\n'
expected: 'Uncaught Error: Whoops!\n'
},
{
command: '(() => { const err = Error(\'Whoops!\'); ' +
'err.foo = \'bar\'; throw err; })()',
expected: "Thrown:\nError: Whoops!\n at repl:*:* {\n foo: 'bar'\n}\n",
expected: "Uncaught Error: Whoops!\n at repl:*:* {\n foo: 'bar'\n}\n",
},
{
command: '(() => { const err = Error(\'Whoops!\'); ' +
'err.foo = \'bar\'; throw err; })()',
expected: 'Thrown:\nError: Whoops!\n at repl:*:* {\n foo: ' +
"\u001b[32m'bar'\u001b[39m\n}\n",
expected: '\u001b[91m\u001b[1m' +
'Uncaught Error: Whoops!\n at repl:*:* {\n foo: ' +
"\u001b[32m'bar'\u001b[39m\n}\n\u001b[39m\u001b[22m",
useColors: true
},
{
command: 'foo = bar;',
expected: 'Thrown:\nReferenceError: bar is not defined\n'
expected: 'Uncaught ReferenceError: bar is not defined\n'
},
// Test anonymous IIFE.
{
command: '(function() { throw new Error(\'Whoops!\'); })()',
expected: 'Thrown:\nError: Whoops!\n at repl:*:*\n'
expected: 'Uncaught Error: Whoops!\n at repl:*:*\n'
}
];

Expand Down
10 changes: 5 additions & 5 deletions test/parallel/test-repl-top-level-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ async function ordinaryTests() {
[ 'if (await true) { function bar() {}; }', 'undefined' ],
[ 'bar', '[Function: bar]' ],
[ 'if (await true) { class Bar {}; }', 'undefined' ],
[ 'Bar', 'ReferenceError: Bar is not defined', { line: 1 } ],
[ 'Bar', 'Uncaught ReferenceError: Bar is not defined' ],
[ 'await 0; function* gen(){}', 'undefined' ],
[ 'for (var i = 0; i < 10; ++i) { await i; }', 'undefined' ],
[ 'i', '10' ],
[ 'for (let j = 0; j < 5; ++j) { await j; }', 'undefined' ],
[ 'j', 'ReferenceError: j is not defined', { line: 1 } ],
[ 'j', 'Uncaught ReferenceError: j is not defined' ],
[ 'gen', '[GeneratorFunction: gen]' ],
[ 'return 42; await 5;', 'SyntaxError: Illegal return statement',
{ line: 4 } ],
[ 'return 42; await 5;', 'Uncaught SyntaxError: Illegal return statement',
{ line: 3 } ],
[ 'let o = await 1, p', 'undefined' ],
[ 'p', 'undefined' ],
[ 'let q = 1, s = await 2', 'undefined' ],
Expand Down Expand Up @@ -160,7 +160,7 @@ async function ctrlCTest() {
{ ctrl: true, name: 'c' }
]), [
'await timeout(100000)\r',
'Thrown:',
'Uncaught:',
'[Error [ERR_SCRIPT_EXECUTION_INTERRUPTED]: ' +
'Script execution was interrupted by `SIGINT`] {',
" code: 'ERR_SCRIPT_EXECUTION_INTERRUPTED'",
Expand Down
3 changes: 1 addition & 2 deletions test/parallel/test-repl-uncaught-exception-standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ child.on('exit', common.mustCall(() => {
[
'Type ".help" for more information.',
// x\n
'> Thrown:',
'ReferenceError: x is not defined',
'> Uncaught ReferenceError: x is not defined',
// Added `uncaughtException` listener.
'> short',
'undefined',
Expand Down
29 changes: 19 additions & 10 deletions test/parallel/test-repl-uncaught-exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const repl = require('repl');

let count = 0;

function run({ command, expected }) {
function run({ command, expected, useColors = false }) {
let accum = '';

const output = new ArrayStream();
Expand All @@ -17,7 +17,7 @@ function run({ command, expected }) {
input: new ArrayStream(),
output,
terminal: false,
useColors: false
useColors
});

r.write(`${command}\n`);
Expand All @@ -30,35 +30,44 @@ function run({ command, expected }) {
// Verify that the repl is still working as expected.
accum = '';
r.write('1 + 1\n');
assert.strictEqual(accum, '2\n');
// eslint-disable-next-line no-control-regex
assert.strictEqual(accum.replace(/\u001b\[[0-9]+m/g, ''), '2\n');
r.close();
count++;
}

const tests = [
{
useColors: true,
command: 'x',
expected: 'Thrown:\n' +
'ReferenceError: x is not defined\n'
expected: '\u001b[91m\u001b[1m' +
'Uncaught ReferenceError: x is not defined\n' +
'\u001b[39m\u001b[22m'
},
{
useColors: true,
command: 'throw { foo: "test" }',
expected: '\u001b[91m\u001b[1m' +
"Uncaught { foo: 'test' }\n" +
'\u001b[39m\u001b[22m'
},
{
command: 'process.on("uncaughtException", () => console.log("Foobar"));\n',
expected: /^Thrown:\nTypeError \[ERR_INVALID_REPL_INPUT]: Listeners for `/
expected: /^Uncaught:\nTypeError \[ERR_INVALID_REPL_INPUT]: Listeners for `/
},
{
command: 'x;\n',
expected: 'Thrown:\n' +
'ReferenceError: x is not defined\n'
expected: 'Uncaught ReferenceError: x is not defined\n'
},
{
command: 'process.on("uncaughtException", () => console.log("Foobar"));' +
'console.log("Baz");\n',
expected: /^Thrown:\nTypeError \[ERR_INVALID_REPL_INPUT]: Listeners for `/
expected: /^Uncaught:\nTypeError \[ERR_INVALID_REPL_INPUT]: Listeners for `/
},
{
command: 'console.log("Baz");' +
'process.on("uncaughtException", () => console.log("Foobar"));\n',
expected: /^Baz\nThrown:\nTypeError \[ERR_INVALID_REPL_INPUT]:.*uncaughtException/
expected: /^Baz\nUncaught:\nTypeError \[ERR_INVALID_REPL_INPUT]:.*uncaughtException/
}
];

Expand Down
12 changes: 4 additions & 8 deletions test/parallel/test-repl-underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,11 @@ function testError() {
'undefined',

// The error, both from the original throw and the `_error` echo.
'Thrown:',
'Error: foo',
'Uncaught Error: foo',
'[Error: foo]',

// The sync error, with individual property echoes
'Thrown:',
/^Error: ENOENT: no such file or directory, scandir '.*nonexistent\?'/,
/^Uncaught Error: ENOENT: no such file or directory, scandir '.*nonexistent\?'/,
/Object\.readdirSync/,
/^ errno: -(2|4058),$/,
" syscall: 'scandir',",
Expand All @@ -194,8 +192,7 @@ function testError() {
'undefined',

// The message from the original throw
'Thrown:',
'Error: baz',
'Uncaught Error: baz',
];
for (const line of lines) {
const expected = expectedLines.shift();
Expand All @@ -218,8 +215,7 @@ function testError() {
"'baz'",
'Expression assignment to _error now disabled.',
'0',
'Thrown:',
'Error: quux',
'Uncaught Error: quux',
'0'
]);
});
Expand Down
Loading