Skip to content

Commit d4bce61

Browse files
lanceaddaleax
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: nodejs/node#15351 Reviewed-By: Prince John Wesley <princejohnwesley@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Refs: nodejs/node#9601
1 parent 4c1b835 commit d4bce61

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
@@ -285,14 +285,16 @@ function REPLServer(prompt,
285285
self._domain.on('error', function debugDomainError(e) {
286286
debug('domain error');
287287
const top = replMap.get(self);
288-
288+
const pstrace = Error.prepareStackTrace;
289+
Error.prepareStackTrace = prepareStackTrace(pstrace);
289290
internalUtil.decorateErrorStack(e);
291+
Error.prepareStackTrace = pstrace;
290292
const isError = internalUtil.isError(e);
291293
if (e instanceof SyntaxError && e.stack) {
292294
// remove repl:line-number and stack trace
293295
e.stack = e.stack
294-
.replace(/^repl:\d+\r?\n/, '')
295-
.replace(/^\s+at\s.*\n?/gm, '');
296+
.replace(/^repl:\d+\r?\n/, '')
297+
.replace(/^\s+at\s.*\n?/gm, '');
296298
} else if (isError && self.replMode === exports.REPL_MODE_STRICT) {
297299
e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/,
298300
(_, pre, line) => pre + (line - 1));
@@ -375,6 +377,30 @@ function REPLServer(prompt,
375377
};
376378
}
377379

380+
function filterInternalStackFrames(error, structuredStack) {
381+
// search from the bottom of the call stack to
382+
// find the first frame with a null function name
383+
if (typeof structuredStack !== 'object')
384+
return structuredStack;
385+
const idx = structuredStack.reverse().findIndex(
386+
(frame) => frame.getFunctionName() === null);
387+
388+
// if found, get rid of it and everything below it
389+
structuredStack = structuredStack.splice(idx + 1);
390+
return structuredStack;
391+
}
392+
393+
function prepareStackTrace(fn) {
394+
return (error, stackFrames) => {
395+
const frames = filterInternalStackFrames(error, stackFrames);
396+
if (fn) {
397+
return fn(error, frames);
398+
}
399+
frames.push(error);
400+
return frames.reverse().join('\n at ');
401+
};
402+
}
403+
378404
function _parseREPLKeyword(keyword, rest) {
379405
var cmd = this.commands[keyword];
380406
if (cmd) {
@@ -942,8 +968,6 @@ function complete(line, callback) {
942968
} else {
943969
const evalExpr = `try { ${expr} } catch (e) {}`;
944970
this.eval(evalExpr, this.context, 'repl', (e, obj) => {
945-
// if (e) console.log(e);
946-
947971
if (obj != null) {
948972
if (typeof obj === 'object' || typeof obj === 'function') {
949973
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
@@ -71,7 +71,7 @@ function clean_up() {
7171
function strict_mode_error_test() {
7272
send_expect([
7373
{ client: client_unix, send: 'ref = 1',
74-
expect: /^ReferenceError:\sref\sis\snot\sdefined\n\s+at\srepl:1:5/ },
74+
expect: /^ReferenceError:\sref\sis\snot\sdefined\nnode via Unix socket> $/ },
7575
]);
7676
}
7777

0 commit comments

Comments
 (0)