Skip to content

Commit b1ffda6

Browse files
BridgeARtargos
authored andcommitted
repl: improve error output
1) Currently extra properties on an error will be ignored, if thrown. This information will from now on be visible. 2) In case someone threw a non error object it would have resulted in `[object Object]`. Instead, the full object will now be visible. 3) Some cases were not detected properly as error before and "Thrown: " was visible before. That is now fixed. PR-URL: #22436 Refs: #20253 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 39d7699 commit b1ffda6

File tree

4 files changed

+53
-24
lines changed

4 files changed

+53
-24
lines changed

lib/repl.js

+41-20
Original file line numberDiff line numberDiff line change
@@ -410,29 +410,50 @@ function REPLServer(prompt,
410410

411411
self._domain.on('error', function debugDomainError(e) {
412412
debug('domain error');
413-
const top = replMap.get(self);
414-
const pstrace = Error.prepareStackTrace;
415-
Error.prepareStackTrace = prepareStackTrace(pstrace);
416-
if (typeof e === 'object')
413+
let errStack = '';
414+
415+
if (typeof e === 'object' && e !== null) {
416+
const pstrace = Error.prepareStackTrace;
417+
Error.prepareStackTrace = prepareStackTrace(pstrace);
417418
internalUtil.decorateErrorStack(e);
418-
Error.prepareStackTrace = pstrace;
419-
const isError = internalUtil.isError(e);
420-
if (!self.underscoreErrAssigned)
421-
self.lastError = e;
422-
if (e instanceof SyntaxError && e.stack) {
423-
// remove repl:line-number and stack trace
424-
e.stack = e.stack
425-
.replace(/^repl:\d+\r?\n/, '')
426-
.replace(/^\s+at\s.*\n?/gm, '');
427-
} else if (isError && self.replMode === exports.REPL_MODE_STRICT) {
428-
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
429-
(_, pre, line) => pre + (line - 1));
419+
Error.prepareStackTrace = pstrace;
420+
421+
if (e.domainThrown) {
422+
delete e.domain;
423+
delete e.domainThrown;
424+
}
425+
426+
if (internalUtil.isError(e)) {
427+
if (e.stack) {
428+
if (e.name === 'SyntaxError') {
429+
// Remove stack trace.
430+
e.stack = e.stack
431+
.replace(/^repl:\d+\r?\n/, '')
432+
.replace(/^\s+at\s.*\n?/gm, '');
433+
} else if (self.replMode === exports.REPL_MODE_STRICT) {
434+
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
435+
(_, pre, line) => pre + (line - 1));
436+
}
437+
}
438+
errStack = util.inspect(e);
439+
440+
// Remove one line error braces to keep the old style in place.
441+
if (errStack[errStack.length - 1] === ']') {
442+
errStack = errStack.slice(1, -1);
443+
}
444+
}
430445
}
431-
if (isError && e.stack) {
432-
top.outputStream.write(`${e.stack}\n`);
433-
} else {
434-
top.outputStream.write(`Thrown: ${String(e)}\n`);
446+
447+
if (errStack === '') {
448+
errStack = `Thrown: ${util.inspect(e)}`;
435449
}
450+
451+
if (!self.underscoreErrAssigned) {
452+
self.lastError = e;
453+
}
454+
455+
const top = replMap.get(self);
456+
top.outputStream.write(`${errStack}\n`);
436457
top.clearBufferedCommand();
437458
top.lines.level = [];
438459
top.displayPrompt();

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ async function ctrlCTest() {
159159
{ ctrl: true, name: 'c' }
160160
]), [
161161
'await timeout(100000)\r',
162-
'Thrown: Error [ERR_SCRIPT_EXECUTION_INTERRUPTED]: ' +
162+
'Error [ERR_SCRIPT_EXECUTION_INTERRUPTED]: ' +
163163
'Script execution was interrupted by `SIGINT`.',
164164
PROMPT
165165
]);

test/parallel/test-repl-underscore.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,12 @@ function testError() {
177177
'[Error: foo]',
178178

179179
// The sync error, with individual property echoes
180-
/Error: ENOENT: no such file or directory, scandir '.*nonexistent.*'/,
180+
/^{ Error: ENOENT: no such file or directory, scandir '.*nonexistent.*'/,
181181
/Object\.readdirSync/,
182+
/^ errno: -(2|4058),$/,
183+
" syscall: 'scandir',",
184+
" code: 'ENOENT',",
185+
" path: '/nonexistent?' }",
182186
"'ENOENT'",
183187
"'scandir'",
184188

test/parallel/test-repl.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ const errorTests = [
132132
// Uncaught error throws and prints out
133133
{
134134
send: 'throw new Error(\'test error\');',
135-
expect: /^Error: test error/
135+
expect: 'Error: test error'
136+
},
137+
{
138+
send: "throw { foo: 'bar' };",
139+
expect: "Thrown: { foo: 'bar' }"
136140
},
137141
// Common syntax error is treated as multiline command
138142
{
@@ -526,7 +530,7 @@ const errorTests = [
526530
{
527531
send: 'require("internal/repl")',
528532
expect: [
529-
/^Error: Cannot find module 'internal\/repl'/,
533+
/^{ Error: Cannot find module 'internal\/repl'/,
530534
/^ at .*/,
531535
/^ at .*/,
532536
/^ at .*/,

0 commit comments

Comments
 (0)