Skip to content

Commit 1ccaf9a

Browse files
BridgeARaddaleax
authored andcommitted
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: #25253 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 5a5bc58 commit 1ccaf9a

8 files changed

+83
-63
lines changed

lib/repl.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -436,15 +436,18 @@ function REPLServer(prompt,
436436
}
437437

438438
if (errStack === '') {
439-
errStack = `Thrown: ${util.inspect(e)}`;
439+
errStack = `Thrown: ${util.inspect(e)}\n`;
440+
} else {
441+
const ln = errStack.endsWith('\n') ? '' : '\n';
442+
errStack = `Thrown:\n${errStack}${ln}`;
440443
}
441444

442445
if (!self.underscoreErrAssigned) {
443446
self.lastError = e;
444447
}
445448

446449
const top = replMap.get(self);
447-
top.outputStream.write(`${errStack}\n`);
450+
top.outputStream.write(errStack);
448451
top.clearBufferedCommand();
449452
top.lines.level = [];
450453
top.displayPrompt();

test/parallel/test-repl-harmony.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const child = spawn(process.execPath, args);
3030
const input = '(function(){"use strict"; const y=1;y=2})()\n';
3131
// This message will vary based on JavaScript engine, so don't check the message
3232
// contents beyond confirming that the `Error` is a `TypeError`.
33-
const expectOut = /^> TypeError: /;
33+
const expectOut = /^> Thrown:\nTypeError: /;
3434

3535
child.stderr.setEncoding('utf8');
3636
child.stderr.on('data', function(c) {

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,26 @@ const tests = [
4646
{
4747
// test .load for a file that throws
4848
command: `.load ${fixtures.path('repl-pretty-stack.js')}`,
49-
expected: 'Error: Whoops!--->\nrepl:9:24--->\nd (repl:12:3)--->\nc ' +
50-
'(repl:9:3)--->\nb (repl:6:3)--->\na (repl:3:3)\n'
49+
expected: 'Thrown:\nError: Whoops!--->\nrepl:9:24--->\nd (repl:12:3)' +
50+
'--->\nc (repl:9:3)--->\nb (repl:6:3)--->\na (repl:3:3)\n'
5151
},
5252
{
5353
command: 'let x y;',
54-
expected: 'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n'
54+
expected: 'Thrown:\n' +
55+
'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n'
5556
},
5657
{
5758
command: 'throw new Error(\'Whoops!\')',
58-
expected: 'Error: Whoops!\n'
59+
expected: 'Thrown:\nError: Whoops!\n'
5960
},
6061
{
6162
command: 'foo = bar;',
62-
expected: 'ReferenceError: bar is not defined\n'
63+
expected: 'Thrown:\nReferenceError: bar is not defined\n'
6364
},
6465
// test anonymous IIFE
6566
{
6667
command: '(function() { throw new Error(\'Whoops!\'); })()',
67-
expected: 'Error: Whoops!--->\nrepl:1:21\n'
68+
expected: 'Thrown:\nError: Whoops!--->\nrepl:1:21\n'
6869
}
6970
];
7071

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,27 @@ const tests = [
3131
{
3232
// test .load for a file that throws
3333
command: `.load ${fixtures.path('repl-pretty-stack.js')}`,
34-
expected: 'Error: Whoops!\n at repl:9:24\n at d (repl:12:3)\n ' +
35-
'at c (repl:9:3)\n at b (repl:6:3)\n at a (repl:3:3)\n'
34+
expected: 'Thrown:\nError: Whoops!\n at repl:9:24\n' +
35+
' at d (repl:12:3)\n at c (repl:9:3)\n' +
36+
' at b (repl:6:3)\n at a (repl:3:3)\n'
3637
},
3738
{
3839
command: 'let x y;',
39-
expected: 'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n\n'
40+
expected: 'Thrown:\n' +
41+
'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n'
4042
},
4143
{
4244
command: 'throw new Error(\'Whoops!\')',
43-
expected: 'Error: Whoops!\n'
45+
expected: 'Thrown:\nError: Whoops!\n'
4446
},
4547
{
4648
command: 'foo = bar;',
47-
expected: 'ReferenceError: bar is not defined\n'
49+
expected: 'Thrown:\nReferenceError: bar is not defined\n'
4850
},
4951
// test anonymous IIFE
5052
{
5153
command: '(function() { throw new Error(\'Whoops!\'); })()',
52-
expected: 'Error: Whoops!\n at repl:1:21\n'
54+
expected: 'Thrown:\nError: Whoops!\n at repl:1:21\n'
5355
}
5456
];
5557

test/parallel/test-repl-tab-complete-no-warn.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ const ArrayStream = require('../common/arraystream');
55
const repl = require('repl');
66
const DEFAULT_MAX_LISTENERS = require('events').defaultMaxListeners;
77

8-
ArrayStream.prototype.write = () => {
9-
};
8+
ArrayStream.prototype.write = () => {};
109

1110
const putIn = new ArrayStream();
1211
const testMe = repl.start('', putIn);

test/parallel/test-repl-top-level-await.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ async function ordinaryTests() {
118118
[ 'if (await true) { function bar() {}; }', 'undefined' ],
119119
[ 'bar', '[Function: bar]' ],
120120
[ 'if (await true) { class Bar {}; }', 'undefined' ],
121-
[ 'Bar', 'ReferenceError: Bar is not defined', { line: 0 } ],
121+
[ 'Bar', 'ReferenceError: Bar is not defined', { line: 1 } ],
122122
[ 'await 0; function* gen(){}', 'undefined' ],
123123
[ 'for (var i = 0; i < 10; ++i) { await i; }', 'undefined' ],
124124
[ 'i', '10' ],
125125
[ 'for (let j = 0; j < 5; ++j) { await j; }', 'undefined' ],
126-
[ 'j', 'ReferenceError: j is not defined', { line: 0 } ],
126+
[ 'j', 'ReferenceError: j is not defined', { line: 1 } ],
127127
[ 'gen', '[GeneratorFunction: gen]' ],
128128
[ 'return 42; await 5;', 'SyntaxError: Illegal return statement',
129-
{ line: 3 } ],
129+
{ line: 4 } ],
130130
[ 'let o = await 1, p', 'undefined' ],
131131
[ 'p', 'undefined' ],
132132
[ 'let q = 1, s = await 2', 'undefined' ],
@@ -160,6 +160,7 @@ async function ctrlCTest() {
160160
{ ctrl: true, name: 'c' }
161161
]), [
162162
'await timeout(100000)\r',
163+
'Thrown:',
163164
'Error [ERR_SCRIPT_EXECUTION_INTERRUPTED]: ' +
164165
'Script execution was interrupted by `SIGINT`',
165166
PROMPT

test/parallel/test-repl-underscore.js

+4
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,12 @@ function testError() {
173173
'undefined',
174174

175175
// The error, both from the original throw and the `_error` echo.
176+
'Thrown:',
176177
'Error: foo',
177178
'[Error: foo]',
178179

179180
// The sync error, with individual property echoes
181+
'Thrown:',
180182
/^{ Error: ENOENT: no such file or directory, scandir '.*nonexistent.*'/,
181183
/Object\.readdirSync/,
182184
/^ errno: -(2|4058),$/,
@@ -191,6 +193,7 @@ function testError() {
191193
'undefined',
192194

193195
// The message from the original throw
196+
'Thrown:',
194197
'Error: baz',
195198
/setImmediate/,
196199
/^ at/,
@@ -217,6 +220,7 @@ function testError() {
217220
"'baz'",
218221
'Expression assignment to _error now disabled.',
219222
'0',
223+
'Thrown:',
220224
'Error: quux',
221225
'0'
222226
]);

0 commit comments

Comments
 (0)