Skip to content

Commit b1dd10d

Browse files
committed
Remove stack trace for syntax error
1 parent 048cf6b commit b1dd10d

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

lib/repl.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,16 +350,15 @@ function REPLServer(prompt,
350350
debug('domain error');
351351
const top = replMap.get(self);
352352
internalUtil.decorateErrorStack(e);
353-
if (e.stack && self.replMode === exports.REPL_MODE_STRICT) {
353+
if (e instanceof SyntaxError && e.stack) {
354+
// remove repl:line-number and stack trace
355+
e.stack = e.stack
356+
.replace(/^repl:\d+\r?\n/, '')
357+
.replace(/^\s+at\s.*\n?/gm, '');
358+
} else if (e.stack && self.replMode === exports.REPL_MODE_STRICT) {
354359
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
355360
(_, pre, line) => pre + (line - 1));
356361
}
357-
if (e instanceof SyntaxError &&
358-
e.stack &&
359-
!e.stack.match(/^SyntaxError:/)) {
360-
// remove filename:line-number
361-
e.stack = e.stack.replace(/^.*?\n/, '');
362-
}
363362
top.outputStream.write((e.stack || e) + '\n');
364363
top.lineParser.reset();
365364
top.bufferedCommand = '';

test/parallel/test-repl.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,12 @@ function error_test() {
333333
expect: "'node'\n" + prompt_unix },
334334
{ client: client_unix, send: 'function name(){ return "nodejs"; };name()',
335335
expect: "'nodejs'\n" + prompt_unix },
336-
// Avoid emitting filename:line-number for SyntaxError
336+
// Avoid emitting repl:line-number for SyntaxError
337337
{ client: client_unix, send: 'a = 3.5e',
338338
expect: /^(?!repl)/ },
339+
// Avoid emitting stack trace
340+
{ client: client_unix, send: 'a = 3.5e',
341+
expect: /^(?!\s+at\s)/ },
339342
]);
340343
}
341344

0 commit comments

Comments
 (0)