mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
Signed-off-by: Aviv Keller <me@aviv.sh> PR-URL: https://github.com/nodejs/node/pull/64034 Fixes: https://github.com/nodejs/node/issues/64523 Closes: https://github.com/nodejs/node/pull/64534 Fixes: https://github.com/nodejs/node/issues/39387 Closes: https://github.com/nodejs/node/pull/39392 Fixes: https://github.com/nodejs/node/issues/61390 Fixes: https://github.com/nodejs/node/issues/63126 Fixes: https://github.com/nodejs/node/issues/38503 Fixes: https://github.com/nodejs/node/issues/37445 Fixes: https://github.com/nodejs/node/issues/38145 Fixes: https://github.com/nodejs/node/issues/33369 Fixes: https://github.com/nodejs/node/issues/48131 Fixes: https://github.com/nodejs/node/issues/8309 Fixes: https://github.com/nodejs/node/issues/39689 Fixes: https://github.com/nodejs/node/issues/18931 Reviewed-By: Dario Piotrowicz <dario.piotrowicz@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
88 lines
2.7 KiB
JavaScript
88 lines
2.7 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
const { start } = require('node:repl');
|
|
const assert = require('node:assert');
|
|
const { PassThrough } = require('node:stream');
|
|
const { once } = require('node:events');
|
|
const test = require('node:test');
|
|
const { spawn } = require('node:child_process');
|
|
|
|
common.skipIfInspectorDisabled();
|
|
|
|
function* generateCases() {
|
|
for (const async of [false, true]) {
|
|
for (const handleErrorReturn of ['ignore', 'print', 'unhandled', 'badvalue']) {
|
|
if (handleErrorReturn === 'badvalue') {
|
|
// Handled through a separate test using a child process
|
|
continue;
|
|
}
|
|
yield { async, handleErrorReturn };
|
|
}
|
|
}
|
|
}
|
|
|
|
for (const { async, handleErrorReturn } of generateCases()) {
|
|
test(`async: ${async}, handleErrorReturn: ${handleErrorReturn}`, async () => {
|
|
let err;
|
|
const options = {
|
|
input: new PassThrough(),
|
|
output: new PassThrough().setEncoding('utf8'),
|
|
handleError: common.mustCall((e) => {
|
|
err = e;
|
|
queueMicrotask(() => repl.emit('handled-error'));
|
|
return handleErrorReturn;
|
|
})
|
|
};
|
|
|
|
let uncaughtExceptionEvent;
|
|
if (handleErrorReturn === 'unhandled' && async) {
|
|
process.removeAllListeners('uncaughtException'); // Remove the test runner's handler
|
|
uncaughtExceptionEvent = once(process, 'uncaughtException');
|
|
}
|
|
|
|
const repl = start(options);
|
|
|
|
let outputString = '';
|
|
options.output.on('data', (chunk) => { outputString += chunk; });
|
|
|
|
const inputString = async ?
|
|
'setImmediate(() => { throw new Error("testerror") })\n42\n' :
|
|
'throw new Error("testerror")\n42\n';
|
|
options.input.end(inputString);
|
|
|
|
await once(repl, 'handled-error');
|
|
assert.strictEqual(err.message, 'testerror');
|
|
while (!/42/.test(outputString)) {
|
|
await once(options.output, 'data');
|
|
}
|
|
|
|
if (handleErrorReturn === 'print') {
|
|
assert.match(outputString, /testerror/);
|
|
} else {
|
|
assert.doesNotMatch(outputString, /testerror/);
|
|
}
|
|
|
|
if (uncaughtExceptionEvent) {
|
|
const [uncaughtErr] = await uncaughtExceptionEvent;
|
|
assert.strictEqual(uncaughtErr, err);
|
|
}
|
|
});
|
|
}
|
|
|
|
test('async: true, handleErrorReturn: badvalue', async () => {
|
|
// Can't test this the same way as the other combinations
|
|
// since this will take the process down in a way that
|
|
// cannot be caught.
|
|
const proc = spawn(process.execPath, ['-e', `
|
|
require('node:repl').start({
|
|
handleError: () => 'badvalue'
|
|
})
|
|
`], { encoding: 'utf8', stdio: 'pipe' });
|
|
proc.stdin.end('throw new Error("foo");');
|
|
let stderr = '';
|
|
proc.stderr.setEncoding('utf8').on('data', (data) => stderr += data);
|
|
const [exit] = await once(proc, 'close');
|
|
assert.strictEqual(exit, 1);
|
|
assert.match(stderr, /ERR_INVALID_STATE.+badvalue/);
|
|
});
|