repl: indicate if errors are thrown or not

Currently an error is printed identical, no matter if it is just
inspected or if the error is thrown inside of the REPL. This makes
sure we are able to distinguish these cases.

PR-URL: https://github.com/nodejs/node/pull/25253
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Ruben Bridgewater 2018-12-28 14:32:16 +01:00 committed by Anna Henningsen
parent 0ff1eb83d9
commit edde065191
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
8 changed files with 83 additions and 63 deletions

View File

@ -435,7 +435,10 @@ function REPLServer(prompt,
} }
if (errStack === '') { if (errStack === '') {
errStack = `Thrown: ${util.inspect(e)}`; errStack = `Thrown: ${util.inspect(e)}\n`;
} else {
const ln = errStack.endsWith('\n') ? '' : '\n';
errStack = `Thrown:\n${errStack}${ln}`;
} }
if (!self.underscoreErrAssigned) { if (!self.underscoreErrAssigned) {
@ -443,7 +446,7 @@ function REPLServer(prompt,
} }
const top = replMap.get(self); const top = replMap.get(self);
top.outputStream.write(`${errStack}\n`); top.outputStream.write(errStack);
top.clearBufferedCommand(); top.clearBufferedCommand();
top.lines.level = []; top.lines.level = [];
top.displayPrompt(); top.displayPrompt();

View File

@ -30,7 +30,7 @@ const child = spawn(process.execPath, args);
const input = '(function(){"use strict"; const y=1;y=2})()\n'; 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 // This message will vary based on JavaScript engine, so don't check the message
// contents beyond confirming that the `Error` is a `TypeError`. // contents beyond confirming that the `Error` is a `TypeError`.
const expectOut = /^> TypeError: /; const expectOut = /^> Thrown:\nTypeError: /;
child.stderr.setEncoding('utf8'); child.stderr.setEncoding('utf8');
child.stderr.on('data', function(c) { child.stderr.on('data', function(c) {

View File

@ -46,25 +46,26 @@ const tests = [
{ {
// test .load for a file that throws // test .load for a file that throws
command: `.load ${fixtures.path('repl-pretty-stack.js')}`, command: `.load ${fixtures.path('repl-pretty-stack.js')}`,
expected: 'Error: Whoops!--->\nrepl:9:24--->\nd (repl:12:3)--->\nc ' + expected: 'Thrown:\nError: Whoops!--->\nrepl:9:24--->\nd (repl:12:3)' +
'(repl:9:3)--->\nb (repl:6:3)--->\na (repl:3:3)\n' '--->\nc (repl:9:3)--->\nb (repl:6:3)--->\na (repl:3:3)\n'
}, },
{ {
command: 'let x y;', command: 'let x y;',
expected: 'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n' expected: 'Thrown:\n' +
'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n'
}, },
{ {
command: 'throw new Error(\'Whoops!\')', command: 'throw new Error(\'Whoops!\')',
expected: 'Error: Whoops!\n' expected: 'Thrown:\nError: Whoops!\n'
}, },
{ {
command: 'foo = bar;', command: 'foo = bar;',
expected: 'ReferenceError: bar is not defined\n' expected: 'Thrown:\nReferenceError: bar is not defined\n'
}, },
// test anonymous IIFE // test anonymous IIFE
{ {
command: '(function() { throw new Error(\'Whoops!\'); })()', command: '(function() { throw new Error(\'Whoops!\'); })()',
expected: 'Error: Whoops!--->\nrepl:1:21\n' expected: 'Thrown:\nError: Whoops!--->\nrepl:1:21\n'
} }
]; ];

View File

@ -31,25 +31,27 @@ const tests = [
{ {
// test .load for a file that throws // test .load for a file that throws
command: `.load ${fixtures.path('repl-pretty-stack.js')}`, command: `.load ${fixtures.path('repl-pretty-stack.js')}`,
expected: 'Error: Whoops!\n at repl:9:24\n at d (repl:12:3)\n ' + expected: 'Thrown:\nError: Whoops!\n at repl:9:24\n' +
'at c (repl:9:3)\n at b (repl:6:3)\n at a (repl:3:3)\n' ' at d (repl:12:3)\n at c (repl:9:3)\n' +
' at b (repl:6:3)\n at a (repl:3:3)\n'
}, },
{ {
command: 'let x y;', command: 'let x y;',
expected: 'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n\n' expected: 'Thrown:\n' +
'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n'
}, },
{ {
command: 'throw new Error(\'Whoops!\')', command: 'throw new Error(\'Whoops!\')',
expected: 'Error: Whoops!\n' expected: 'Thrown:\nError: Whoops!\n'
}, },
{ {
command: 'foo = bar;', command: 'foo = bar;',
expected: 'ReferenceError: bar is not defined\n' expected: 'Thrown:\nReferenceError: bar is not defined\n'
}, },
// test anonymous IIFE // test anonymous IIFE
{ {
command: '(function() { throw new Error(\'Whoops!\'); })()', command: '(function() { throw new Error(\'Whoops!\'); })()',
expected: 'Error: Whoops!\n at repl:1:21\n' expected: 'Thrown:\nError: Whoops!\n at repl:1:21\n'
} }
]; ];

View File

@ -5,8 +5,7 @@ const ArrayStream = require('../common/arraystream');
const repl = require('repl'); const repl = require('repl');
const DEFAULT_MAX_LISTENERS = require('events').defaultMaxListeners; const DEFAULT_MAX_LISTENERS = require('events').defaultMaxListeners;
ArrayStream.prototype.write = () => { ArrayStream.prototype.write = () => {};
};
const putIn = new ArrayStream(); const putIn = new ArrayStream();
const testMe = repl.start('', putIn); const testMe = repl.start('', putIn);

View File

@ -118,15 +118,15 @@ async function ordinaryTests() {
[ 'if (await true) { function bar() {}; }', 'undefined' ], [ 'if (await true) { function bar() {}; }', 'undefined' ],
[ 'bar', '[Function: bar]' ], [ 'bar', '[Function: bar]' ],
[ 'if (await true) { class Bar {}; }', 'undefined' ], [ 'if (await true) { class Bar {}; }', 'undefined' ],
[ 'Bar', 'ReferenceError: Bar is not defined', { line: 0 } ], [ 'Bar', 'ReferenceError: Bar is not defined', { line: 1 } ],
[ 'await 0; function* gen(){}', 'undefined' ], [ 'await 0; function* gen(){}', 'undefined' ],
[ 'for (var i = 0; i < 10; ++i) { await i; }', 'undefined' ], [ 'for (var i = 0; i < 10; ++i) { await i; }', 'undefined' ],
[ 'i', '10' ], [ 'i', '10' ],
[ 'for (let j = 0; j < 5; ++j) { await j; }', 'undefined' ], [ 'for (let j = 0; j < 5; ++j) { await j; }', 'undefined' ],
[ 'j', 'ReferenceError: j is not defined', { line: 0 } ], [ 'j', 'ReferenceError: j is not defined', { line: 1 } ],
[ 'gen', '[GeneratorFunction: gen]' ], [ 'gen', '[GeneratorFunction: gen]' ],
[ 'return 42; await 5;', 'SyntaxError: Illegal return statement', [ 'return 42; await 5;', 'SyntaxError: Illegal return statement',
{ line: 3 } ], { line: 4 } ],
[ 'let o = await 1, p', 'undefined' ], [ 'let o = await 1, p', 'undefined' ],
[ 'p', 'undefined' ], [ 'p', 'undefined' ],
[ 'let q = 1, s = await 2', 'undefined' ], [ 'let q = 1, s = await 2', 'undefined' ],
@ -160,6 +160,7 @@ async function ctrlCTest() {
{ ctrl: true, name: 'c' } { ctrl: true, name: 'c' }
]), [ ]), [
'await timeout(100000)\r', 'await timeout(100000)\r',
'Thrown:',
'Error [ERR_SCRIPT_EXECUTION_INTERRUPTED]: ' + 'Error [ERR_SCRIPT_EXECUTION_INTERRUPTED]: ' +
'Script execution was interrupted by `SIGINT`', 'Script execution was interrupted by `SIGINT`',
PROMPT PROMPT

View File

@ -173,10 +173,12 @@ function testError() {
'undefined', 'undefined',
// The error, both from the original throw and the `_error` echo. // The error, both from the original throw and the `_error` echo.
'Thrown:',
'Error: foo', 'Error: foo',
'[Error: foo]', '[Error: foo]',
// The sync error, with individual property echoes // The sync error, with individual property echoes
'Thrown:',
/^{ Error: ENOENT: no such file or directory, scandir '.*nonexistent.*'/, /^{ Error: ENOENT: no such file or directory, scandir '.*nonexistent.*'/,
/Object\.readdirSync/, /Object\.readdirSync/,
/^ errno: -(2|4058),$/, /^ errno: -(2|4058),$/,
@ -191,6 +193,7 @@ function testError() {
'undefined', 'undefined',
// The message from the original throw // The message from the original throw
'Thrown:',
'Error: baz', 'Error: baz',
/setImmediate/, /setImmediate/,
/^ at/, /^ at/,
@ -217,6 +220,7 @@ function testError() {
"'baz'", "'baz'",
'Expression assignment to _error now disabled.', 'Expression assignment to _error now disabled.',
'0', '0',
'Thrown:',
'Error: quux', 'Error: quux',
'0' '0'
]); ]);

View File

@ -25,6 +25,7 @@ const fixtures = require('../common/fixtures');
const assert = require('assert'); const assert = require('assert');
const net = require('net'); const net = require('net');
const repl = require('repl'); const repl = require('repl');
const { inspect } = require('util');
const message = 'Read, Eval, Print Loop'; const message = 'Read, Eval, Print Loop';
const prompt_unix = 'node via Unix socket> '; const prompt_unix = 'node via Unix socket> ';
@ -58,7 +59,7 @@ async function runReplTests(socket, prompt, tests) {
expectedLine = send; expectedLine = send;
while (!lineBuffer.includes('\n')) { while (!lineBuffer.includes('\n')) {
lineBuffer += await event(socket, 'data'); lineBuffer += await event(socket, expect);
// Cut away the initial prompt // Cut away the initial prompt
while (lineBuffer.startsWith(prompt)) while (lineBuffer.startsWith(prompt))
@ -124,7 +125,7 @@ const unixTests = [
const strictModeTests = [ const strictModeTests = [
{ {
send: 'ref = 1', send: 'ref = 1',
expect: /^ReferenceError:\s/ expect: ['Thrown:', /^ReferenceError:\s/]
} }
]; ];
@ -132,7 +133,7 @@ const errorTests = [
// Uncaught error throws and prints out // Uncaught error throws and prints out
{ {
send: 'throw new Error(\'test error\');', send: 'throw new Error(\'test error\');',
expect: 'Error: test error' expect: ['Thrown:', 'Error: test error']
}, },
{ {
send: "throw { foo: 'bar' };", send: "throw { foo: 'bar' };",
@ -151,7 +152,7 @@ const errorTests = [
// But passing the same string to eval() should throw // But passing the same string to eval() should throw
{ {
send: 'eval("function test_func() {")', send: 'eval("function test_func() {")',
expect: /^SyntaxError: / expect: ['Thrown:', /^SyntaxError: /]
}, },
// Can handle multiline template literals // Can handle multiline template literals
{ {
@ -208,90 +209,90 @@ const errorTests = [
// should throw // should throw
{ {
send: 'JSON.parse(\'{invalid: \\\'json\\\'}\');', send: 'JSON.parse(\'{invalid: \\\'json\\\'}\');',
expect: [/^SyntaxError: /, ''] expect: ['Thrown:', /^SyntaxError: /]
}, },
// End of input to JSON.parse error is special case of syntax error, // End of input to JSON.parse error is special case of syntax error,
// should throw // should throw
{ {
send: 'JSON.parse(\'066\');', send: 'JSON.parse(\'066\');',
expect: [/^SyntaxError: /, ''] expect: ['Thrown:', /^SyntaxError: /]
}, },
// should throw // should throw
{ {
send: 'JSON.parse(\'{\');', send: 'JSON.parse(\'{\');',
expect: [/^SyntaxError: /, ''] expect: ['Thrown:', /^SyntaxError: /]
}, },
// invalid RegExps are a special case of syntax error, // invalid RegExps are a special case of syntax error,
// should throw // should throw
{ {
send: '/(/;', send: '/(/;',
expect: /^SyntaxError: / expect: ['Thrown:', /^SyntaxError: /]
}, },
// invalid RegExp modifiers are a special case of syntax error, // invalid RegExp modifiers are a special case of syntax error,
// should throw (GH-4012) // should throw (GH-4012)
{ {
send: 'new RegExp("foo", "wrong modifier");', send: 'new RegExp("foo", "wrong modifier");',
expect: [/^SyntaxError: /, ''] expect: ['Thrown:', /^SyntaxError: /]
}, },
// Strict mode syntax errors should be caught (GH-5178) // Strict mode syntax errors should be caught (GH-5178)
{ {
send: '(function() { "use strict"; return 0755; })()', send: '(function() { "use strict"; return 0755; })()',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
{ {
send: '(function(a, a, b) { "use strict"; return a + b + c; })()', send: '(function(a, a, b) { "use strict"; return a + b + c; })()',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
{ {
send: '(function() { "use strict"; with (this) {} })()', send: '(function() { "use strict"; with (this) {} })()',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
{ {
send: '(function() { "use strict"; var x; delete x; })()', send: '(function() { "use strict"; var x; delete x; })()',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
{ {
send: '(function() { "use strict"; eval = 17; })()', send: '(function() { "use strict"; eval = 17; })()',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
{ {
send: '(function() { "use strict"; if (true) function f() { } })()', send: '(function() { "use strict"; if (true) function f() { } })()',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
// Named functions can be used: // Named functions can be used:
@ -396,11 +397,11 @@ const errorTests = [
{ {
send: '[] \\', send: '[] \\',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
// Do not fail when a String is created with line continuation // Do not fail when a String is created with line continuation
@ -530,6 +531,7 @@ const errorTests = [
{ {
send: 'require("internal/repl")', send: 'require("internal/repl")',
expect: [ expect: [
'Thrown:',
/^{ Error: Cannot find module 'internal\/repl'/, /^{ Error: Cannot find module 'internal\/repl'/,
/^ at .*/, /^ at .*/,
/^ at .*/, /^ at .*/,
@ -563,11 +565,11 @@ const errorTests = [
{ {
send: 'a = 3.5e', send: 'a = 3.5e',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
// Mitigate https://github.com/nodejs/node/issues/548 // Mitigate https://github.com/nodejs/node/issues/548
@ -583,22 +585,22 @@ const errorTests = [
{ {
send: 'a = 3.5e', send: 'a = 3.5e',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
// Avoid emitting stack trace // Avoid emitting stack trace
{ {
send: 'a = 3.5e', send: 'a = 3.5e',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
@ -663,11 +665,11 @@ const errorTests = [
{ {
send: '...[]', send: '...[]',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
// bring back the repl to prompt // bring back the repl to prompt
@ -678,31 +680,31 @@ const errorTests = [
{ {
send: 'console.log("Missing comma in arg list" process.version)', send: 'console.log("Missing comma in arg list" process.version)',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
{ {
send: 'x = {\nfield\n{', send: 'x = {\nfield\n{',
expect: [ expect: [
'... ... {', '... ... Thrown:',
'{',
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
{ {
send: '(2 + 3))', send: '(2 + 3))',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
{ {
@ -716,11 +718,11 @@ const errorTests = [
{ {
send: '} else {', send: '} else {',
expect: [ expect: [
'Thrown:',
kSource, kSource,
kArrow, kArrow,
'', '',
/^SyntaxError: /, /^SyntaxError: /
''
] ]
}, },
]; ];
@ -844,8 +846,16 @@ function startUnixRepl() {
]); ]);
} }
function event(ee, eventName) { function event(ee, expected) {
return new Promise((resolve) => { return new Promise((resolve, reject) => {
ee.once(eventName, common.mustCall(resolve)); const timeout = setTimeout(() => {
const data = inspect(expected, { compact: false });
const msg = `The REPL did not reply as expected for:\n\n${data}`;
reject(new Error(msg));
}, 500);
ee.once('data', common.mustCall((...args) => {
clearTimeout(timeout);
resolve(...args);
}));
}); });
} }