-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
repl: handle uncaughtException properly
When running the REPL as standalone program it's now possible to use `process.on('uncaughtException', listener)`. It is going to use those listeners from now on and the regular error output is suppressed. It also fixes the issue that REPL instances started inside of an application would silence all application errors. It is now prohibited to add the exception listener in such REPL instances. Trying to add such listeners throws an `ERR_INVALID_REPL_INPUT` error. Fixes: #19998 PR-URL: #27151 Fixes: #19998 Reviewed-By: Lance Ball <[email protected]> Reviewed-By: Rich Trott <[email protected]>
- Loading branch information
Showing
8 changed files
with
244 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
|
||
// This verifies that adding an `uncaughtException` listener in an REPL instance | ||
// does not suppress errors in the whole application. Adding such listener | ||
// should throw. | ||
|
||
require('../common'); | ||
const ArrayStream = require('../common/arraystream'); | ||
const repl = require('repl'); | ||
const assert = require('assert'); | ||
|
||
let accum = ''; | ||
|
||
const output = new ArrayStream(); | ||
output.write = (data) => accum += data.replace('\r', ''); | ||
|
||
const r = repl.start({ | ||
prompt: '', | ||
input: new ArrayStream(), | ||
output, | ||
terminal: false, | ||
useColors: false, | ||
global: false | ||
}); | ||
|
||
r.write( | ||
'process.nextTick(() => {\n' + | ||
' process.on("uncaughtException", () => console.log("Foo"));\n' + | ||
' throw new TypeError("foobar");\n' + | ||
'});\n' | ||
); | ||
r.write( | ||
'setTimeout(() => {\n' + | ||
' throw new RangeError("abc");\n' + | ||
'}, 1);console.log()\n' | ||
); | ||
r.close(); | ||
|
||
setTimeout(() => { | ||
const len = process.listenerCount('uncaughtException'); | ||
process.removeAllListeners('uncaughtException'); | ||
assert.strictEqual(len, 0); | ||
assert(/ERR_INVALID_REPL_INPUT.*(?!Type)RangeError: abc/s.test(accum)); | ||
}, 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const cp = require('child_process'); | ||
const child = cp.spawn(process.execPath, ['-i']); | ||
let output = ''; | ||
|
||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', (data) => { | ||
output += data; | ||
}); | ||
|
||
child.on('exit', common.mustCall(() => { | ||
const results = output.split('\n'); | ||
results.shift(); | ||
assert.deepStrictEqual( | ||
results, | ||
[ | ||
'Type ".help" for more information.', | ||
// x\n | ||
'> Thrown:', | ||
'ReferenceError: x is not defined', | ||
// Added `uncaughtException` listener. | ||
'> short', | ||
'undefined', | ||
// x\n | ||
'> Foobar', | ||
'> ' | ||
] | ||
); | ||
})); | ||
|
||
child.stdin.write('x\n'); | ||
child.stdin.write( | ||
'process.on("uncaughtException", () => console.log("Foobar"));' + | ||
'console.log("short")\n'); | ||
child.stdin.write('x\n'); | ||
child.stdin.end(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
require('../common'); | ||
const ArrayStream = require('../common/arraystream'); | ||
const assert = require('assert'); | ||
const repl = require('repl'); | ||
|
||
let count = 0; | ||
|
||
function run({ command, expected }) { | ||
let accum = ''; | ||
|
||
const output = new ArrayStream(); | ||
output.write = (data) => accum += data.replace('\r', ''); | ||
|
||
const r = repl.start({ | ||
prompt: '', | ||
input: new ArrayStream(), | ||
output, | ||
terminal: false, | ||
useColors: false | ||
}); | ||
|
||
r.write(`${command}\n`); | ||
if (typeof expected === 'string') { | ||
assert.strictEqual(accum, expected); | ||
} else { | ||
assert(expected.test(accum), accum); | ||
} | ||
|
||
// Verify that the repl is still working as expected. | ||
accum = ''; | ||
r.write('1 + 1\n'); | ||
assert.strictEqual(accum, '2\n'); | ||
r.close(); | ||
count++; | ||
} | ||
|
||
const tests = [ | ||
{ | ||
command: 'x', | ||
expected: 'Thrown:\n' + | ||
'ReferenceError: x is not defined\n' | ||
}, | ||
{ | ||
command: 'process.on("uncaughtException", () => console.log("Foobar"));\n', | ||
expected: /^Thrown:\nTypeError \[ERR_INVALID_REPL_INPUT]: Listeners for `/ | ||
}, | ||
{ | ||
command: 'x;\n', | ||
expected: 'Thrown:\n' + | ||
'ReferenceError: x is not defined\n' | ||
}, | ||
{ | ||
command: 'process.on("uncaughtException", () => console.log("Foobar"));' + | ||
'console.log("Baz");\n', | ||
expected: /^Thrown:\nTypeError \[ERR_INVALID_REPL_INPUT]: Listeners for `/ | ||
}, | ||
{ | ||
command: 'console.log("Baz");' + | ||
'process.on("uncaughtException", () => console.log("Foobar"));\n', | ||
expected: /^Baz\nThrown:\nTypeError \[ERR_INVALID_REPL_INPUT]:.*uncaughtException/ | ||
} | ||
]; | ||
|
||
process.on('exit', () => { | ||
// To actually verify that the test passed we have to make sure no | ||
// `uncaughtException` listeners exist anymore. | ||
process.removeAllListeners('uncaughtException'); | ||
assert.strictEqual(count, tests.length); | ||
}); | ||
|
||
tests.forEach(run); |