Skip to content

Commit 6deae6e

Browse files
lancegibfahn
authored andcommitted
repl: remove internal frames from runtime errors
When a user executes code in the REPLServer which generates an exception, there is no need to display the REPLServer internal stack frames. PR-URL: #15351 Backport-PR-URL: #16457 Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Refs: #9601
1 parent 6fa51d6 commit 6deae6e

File tree

5 files changed

+174
-6
lines changed

5 files changed

+174
-6
lines changed

lib/repl.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,16 @@ function REPLServer(prompt,
284284
self._domain.on('error', function debugDomainError(e) {
285285
debug('domain error');
286286
const top = replMap.get(self);
287-
287+
const pstrace = Error.prepareStackTrace;
288+
Error.prepareStackTrace = prepareStackTrace(pstrace);
288289
internalUtil.decorateErrorStack(e);
290+
Error.prepareStackTrace = pstrace;
289291
const isError = internalUtil.isError(e);
290292
if (e instanceof SyntaxError && e.stack) {
291293
// remove repl:line-number and stack trace
292294
e.stack = e.stack
293-
.replace(/^repl:\d+\r?\n/, '')
294-
.replace(/^\s+at\s.*\n?/gm, '');
295+
.replace(/^repl:\d+\r?\n/, '')
296+
.replace(/^\s+at\s.*\n?/gm, '');
295297
} else if (isError && self.replMode === exports.REPL_MODE_STRICT) {
296298
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
297299
(_, pre, line) => pre + (line - 1));
@@ -366,6 +368,30 @@ function REPLServer(prompt,
366368
};
367369
}
368370

371+
function filterInternalStackFrames(error, structuredStack) {
372+
// search from the bottom of the call stack to
373+
// find the first frame with a null function name
374+
if (typeof structuredStack !== 'object')
375+
return structuredStack;
376+
const idx = structuredStack.reverse().findIndex(
377+
(frame) => frame.getFunctionName() === null);
378+
379+
// if found, get rid of it and everything below it
380+
structuredStack = structuredStack.splice(idx + 1);
381+
return structuredStack;
382+
}
383+
384+
function prepareStackTrace(fn) {
385+
return (error, stackFrames) => {
386+
const frames = filterInternalStackFrames(error, stackFrames);
387+
if (fn) {
388+
return fn(error, frames);
389+
}
390+
frames.push(error);
391+
return frames.reverse().join('\n at ');
392+
};
393+
}
394+
369395
self.on('close', function emitExit() {
370396
self.emit('exit');
371397
});
@@ -916,8 +942,6 @@ function complete(line, callback) {
916942
} else {
917943
const evalExpr = `try { ${expr} } catch (e) {}`;
918944
this.eval(evalExpr, this.context, 'repl', (e, obj) => {
919-
// if (e) console.log(e);
920-
921945
if (obj != null) {
922946
if (typeof obj === 'object' || typeof obj === 'function') {
923947
try {

test/fixtures/repl-pretty-stack.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
function a() {
4+
b();
5+
}
6+
7+
function b() {
8+
c();
9+
}
10+
11+
function c() {
12+
d(function() { throw new Error('Whoops!'); });
13+
}
14+
15+
function d(f) {
16+
f();
17+
}
18+
19+
a();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict';
2+
const common = require('../common');
3+
const fixtures = require('../common/fixtures');
4+
const assert = require('assert');
5+
const repl = require('repl');
6+
7+
8+
function run({ command, expected }) {
9+
let accum = '';
10+
11+
const inputStream = new common.ArrayStream();
12+
const outputStream = new common.ArrayStream();
13+
14+
outputStream.write = (data) => accum += data.replace('\r', '');
15+
16+
const r = repl.start({
17+
prompt: '',
18+
input: inputStream,
19+
output: outputStream,
20+
terminal: false,
21+
useColors: false
22+
});
23+
24+
r.write(`${command}\n`);
25+
assert.strictEqual(accum, expected);
26+
r.close();
27+
}
28+
29+
const origPrepareStackTrace = Error.prepareStackTrace;
30+
Error.prepareStackTrace = (err, stack) => {
31+
if (err instanceof SyntaxError)
32+
return err.toString();
33+
stack.push(err);
34+
return stack.reverse().join('--->\n');
35+
};
36+
37+
process.on('uncaughtException', (e) => {
38+
Error.prepareStackTrace = origPrepareStackTrace;
39+
throw e;
40+
});
41+
42+
process.on('exit', () => (Error.prepareStackTrace = origPrepareStackTrace));
43+
44+
const tests = [
45+
{
46+
// test .load for a file that throws
47+
command: `.load ${fixtures.path('repl-pretty-stack.js')}`,
48+
expected: 'Error: Whoops!--->\nrepl:9:24--->\nd (repl:12:3)--->\nc ' +
49+
'(repl:9:3)--->\nb (repl:6:3)--->\na (repl:3:3)\n'
50+
},
51+
{
52+
command: 'let x y;',
53+
expected: 'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n'
54+
},
55+
{
56+
command: 'throw new Error(\'Whoops!\')',
57+
expected: 'Error: Whoops!\n'
58+
},
59+
{
60+
command: 'foo = bar;',
61+
expected: 'ReferenceError: bar is not defined\n'
62+
},
63+
// test anonymous IIFE
64+
{
65+
command: '(function() { throw new Error(\'Whoops!\'); })()',
66+
expected: 'Error: Whoops!--->\nrepl:1:21\n'
67+
}
68+
];
69+
70+
tests.forEach(run);
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
const common = require('../common');
3+
const fixtures = require('../common/fixtures');
4+
const assert = require('assert');
5+
const repl = require('repl');
6+
7+
8+
function run({ command, expected }) {
9+
let accum = '';
10+
11+
const inputStream = new common.ArrayStream();
12+
const outputStream = new common.ArrayStream();
13+
14+
outputStream.write = (data) => accum += data.replace('\r', '');
15+
16+
const r = repl.start({
17+
prompt: '',
18+
input: inputStream,
19+
output: outputStream,
20+
terminal: false,
21+
useColors: false
22+
});
23+
24+
r.write(`${command}\n`);
25+
assert.strictEqual(accum, expected);
26+
r.close();
27+
}
28+
29+
const tests = [
30+
{
31+
// test .load for a file that throws
32+
command: `.load ${fixtures.path('repl-pretty-stack.js')}`,
33+
expected: 'Error: Whoops!\n at repl:9:24\n at d (repl:12:3)\n ' +
34+
'at c (repl:9:3)\n at b (repl:6:3)\n at a (repl:3:3)\n'
35+
},
36+
{
37+
command: 'let x y;',
38+
expected: 'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n\n'
39+
},
40+
{
41+
command: 'throw new Error(\'Whoops!\')',
42+
expected: 'Error: Whoops!\n'
43+
},
44+
{
45+
command: 'foo = bar;',
46+
expected: 'ReferenceError: bar is not defined\n'
47+
},
48+
// test anonymous IIFE
49+
{
50+
command: '(function() { throw new Error(\'Whoops!\'); })()',
51+
expected: 'Error: Whoops!\n at repl:1:21\n'
52+
}
53+
];
54+
55+
tests.forEach(run);

test/parallel/test-repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function clean_up() {
7272
function strict_mode_error_test() {
7373
send_expect([
7474
{ client: client_unix, send: 'ref = 1',
75-
expect: /^ReferenceError:\sref\sis\snot\sdefined\n\s+at\srepl:1:5/ },
75+
expect: /^ReferenceError:\sref\sis\snot\sdefined\nnode via Unix socket> $/ },
7676
]);
7777
}
7878

0 commit comments

Comments
 (0)