Improve IPC error messages (#1090)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 18e607c · Browse files · 5 files·+42 added−29 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+42 added−29 removed
- tests:+39 added−26 removed
- other:+3 added−3 removed
Provenance
- Commit
e6b012a- Tree
86df193- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
18e607c
5 files changed+42 added−29 removed
modified:lib/ipc/ipc-input.js+2 added-2 removed
15 unmodified lines1617181920202122232425262728282930313212 unmodified lines15 unmodified linesconst validateAdvancedInput = ipcInput => { try { serialize(ipcInput); } catch (error) { throw new Error(`The \`ipcInput\` option is not serializable with a structured clone.\n${error.message}`); throw new Error('The `ipcInput` option is not serializable with a structured clone.', {cause: error}); }};
const validateJsonInput = ipcInput => { try { JSON.stringify(ipcInput); } catch (error) { throw new Error(`The \`ipcInput\` option is not serializable with JSON.\n${error.message}`); throw new Error('The `ipcInput` option is not serializable with JSON.', {cause: error}); }};
const validateIpcInput = {12 unmodified linesmodified:lib/ipc/validation.js+1 added-1 removed
37 unmodified lines3839404142424344454628 unmodified lines37 unmodified lines// Better error message when sending messages which cannot be serialized.// Works with both `serialization: 'advanced'` and `serialization: 'json'`.export const handleSerializationError = (error, isSubprocess, message) => { if (isSerializationError(error)) { error.message = `${getNamespaceName(isSubprocess)}sendMessage()'s argument type is invalid: the message cannot be serialized: ${String(message)}.\n${error.message}`; throw new Error(`${getNamespaceName(isSubprocess)}sendMessage()'s argument type is invalid: the message cannot be serialized: ${String(message)}.`, {cause: error}); }};
const isSerializationError = ({code, message}) => SERIALIZATION_ERROR_CODES.has(code)28 unmodified linesmodified:test/ipc/ipc-input.js+9 added-7 removed
24 unmodified lines2526272829293031323233343536363738393940414243444344454546474824 unmodified lines }, {message: /The "ipcInput" option cannot be used with synchronous/});});
test('Invalid "ipcInput" option v8 format', t => { const {message} = t.throws(() => { const {message, cause} = t.throws(() => { execa('empty.js', {ipcInput() {}}); }); t.is(message, 'The `ipcInput` option is not serializable with a structured clone.\nipcInput() {} could not be cloned.'); t.is(message, 'The `ipcInput` option is not serializable with a structured clone.'); t.is(cause.message, 'ipcInput() {} could not be cloned.');});
test('Invalid "ipcInput" option JSON format', t => { const {message} = t.throws(() => { const {message, cause} = t.throws(() => { execa('empty.js', {ipcInput: 0n, serialization: 'json'}); }); t.is(message, 'The `ipcInput` option is not serializable with JSON.\nDo not know how to serialize a BigInt'); t.is(message, 'The `ipcInput` option is not serializable with JSON.'); t.is(cause.message, 'Do not know how to serialize a BigInt');});
test('Handles "ipcInput" option during sending', async t => { await t.throwsAsync(execa('empty.js', {ipcInput: 0n}), { message: /The "message" argument must be one of type string/, }); const {message, cause} = await t.throwsAsync(execa('empty.js', {ipcInput: 0n})); t.true(message.includes('subprocess.sendMessage()\'s argument type is invalid: the message cannot be serialized: 0.')); t.true(cause.cause.message.includes('The "message" argument must be one of type string'));});modified:test/ipc/send.js+5 added-3 removed
60 unmodified lines61 const subprocess = execa('ipc-get-send-get.js', ['false'], {ipc: true});62 await subprocess.sendMessage(foobarString);63 t.is(await subprocess.getOneMessage(), foobarString);64 65- const {message} = await t.throwsAsync(subprocess.sendMessage(0n));66- t.true(message.includes('subprocess.sendMessage()\'s argument type is invalid'));65+ const {message, cause} = await t.throwsAsync(subprocess.sendMessage(0n));66+ t.is(message, 'subprocess.sendMessage()\'s argument type is invalid: the message cannot be serialized: 0.');67+ t.true(cause.message.includes('The "message" argument must be one of type string'));68 69 const {exitCode, isTerminated, stderr} = await t.throwsAsync(subprocess);70 t.is(exitCode, 1);71 t.false(isTerminated);7 unmodified lines79 80 const {exitCode, isTerminated, stderr} = await t.throwsAsync(subprocess);81 t.is(exitCode, 1);82 t.false(isTerminated);82- t.true(stderr.includes('sendMessage()\'s argument type is invalid'));83+ t.true(stderr.includes('Error: sendMessage()\'s argument type is invalid: the message cannot be serialized: 0.'));84+ t.true(stderr.includes('The "message" argument must be one of type string'));85 });86 87 // EPIPE happens based on timing conditions, so we must repeat it until it happens88 const findEpipeError = async t => {23 unmodified linesmodified:test/ipc/validation.js+25 added-16 removed
57 unmodified lines58 test('exports.sendMessage() after disconnection', testPostDisconnectionSubprocess, 'sendMessage');59 test('exports.getOneMessage() after disconnection', testPostDisconnectionSubprocess, 'getOneMessage');60 test('exports.getEachMessage() after disconnection', testPostDisconnectionSubprocess, 'getEachMessage');61 62+const INVALID_TYPE_MESSAGE = 'The "message" argument must be one of type string';63+const UNDEFINED_MESSAGE = 'The "message" argument must be specified';64+const CLONE_MESSAGE = 'could not be cloned';65+const CYCLE_MESSAGE = 'Converting circular structure to JSON';66+const MAX_CALL_STACK_MESSAGE = 'Maximum call stack size exceeded';67+62-const testInvalidPayload = async (t, serialization, message) => {68+const testInvalidPayload = async (t, serialization, message, expectedMessage) => {69 const subprocess = execa('empty.js', {ipc: true, serialization});64- await t.throwsAsync(subprocess.sendMessage(message), {message: /type is invalid/});70+ const error = await t.throwsAsync(subprocess.sendMessage(message));71+ t.true(error.message.includes('subprocess.sendMessage()\'s argument type is invalid: the message cannot be serialized'));72+ t.true(error.cause.message.includes(expectedMessage));73 await subprocess;74 };75 76 const cycleObject = {};77 cycleObject.self = cycleObject;78 const toJsonCycle = {toJSON: () => ({test: true, toJsonCycle})};79 72-test('subprocess.sendMessage() cannot send undefined', testInvalidPayload, 'advanced', undefined);73-test('subprocess.sendMessage() cannot send bigints', testInvalidPayload, 'advanced', 0n);74-test('subprocess.sendMessage() cannot send symbols', testInvalidPayload, 'advanced', Symbol('test'));75-test('subprocess.sendMessage() cannot send functions', testInvalidPayload, 'advanced', () => {});76-test('subprocess.sendMessage() cannot send promises', testInvalidPayload, 'advanced', Promise.resolve());77-test('subprocess.sendMessage() cannot send proxies', testInvalidPayload, 'advanced', new Proxy({}, {}));78-test('subprocess.sendMessage() cannot send Intl', testInvalidPayload, 'advanced', new Intl.Collator());79-test('subprocess.sendMessage() cannot send undefined, JSON', testInvalidPayload, 'json', undefined);80-test('subprocess.sendMessage() cannot send bigints, JSON', testInvalidPayload, 'json', 0n);81-test('subprocess.sendMessage() cannot send symbols, JSON', testInvalidPayload, 'json', Symbol('test'));82-test('subprocess.sendMessage() cannot send functions, JSON', testInvalidPayload, 'json', () => {});83-test('subprocess.sendMessage() cannot send cycles, JSON', testInvalidPayload, 'json', cycleObject);84-test('subprocess.sendMessage() cannot send cycles in toJSON(), JSON', testInvalidPayload, 'json', toJsonCycle);80+test('subprocess.sendMessage() cannot send undefined', testInvalidPayload, 'advanced', undefined, UNDEFINED_MESSAGE);81+test('subprocess.sendMessage() cannot send bigints', testInvalidPayload, 'advanced', 0n, INVALID_TYPE_MESSAGE);82+test('subprocess.sendMessage() cannot send symbols', testInvalidPayload, 'advanced', Symbol('test'), INVALID_TYPE_MESSAGE);83+test('subprocess.sendMessage() cannot send functions', testInvalidPayload, 'advanced', () => {}, INVALID_TYPE_MESSAGE);84+test('subprocess.sendMessage() cannot send promises', testInvalidPayload, 'advanced', Promise.resolve(), CLONE_MESSAGE);85+test('subprocess.sendMessage() cannot send proxies', testInvalidPayload, 'advanced', new Proxy({}, {}), CLONE_MESSAGE);86+test('subprocess.sendMessage() cannot send Intl', testInvalidPayload, 'advanced', new Intl.Collator(), CLONE_MESSAGE);87+test('subprocess.sendMessage() cannot send undefined, JSON', testInvalidPayload, 'json', undefined, UNDEFINED_MESSAGE);88+test('subprocess.sendMessage() cannot send bigints, JSON', testInvalidPayload, 'json', 0n, INVALID_TYPE_MESSAGE);89+test('subprocess.sendMessage() cannot send symbols, JSON', testInvalidPayload, 'json', Symbol('test'), INVALID_TYPE_MESSAGE);90+test('subprocess.sendMessage() cannot send functions, JSON', testInvalidPayload, 'json', () => {}, INVALID_TYPE_MESSAGE);91+test('subprocess.sendMessage() cannot send cycles, JSON', testInvalidPayload, 'json', cycleObject, CYCLE_MESSAGE);92+test('subprocess.sendMessage() cannot send cycles in toJSON(), JSON', testInvalidPayload, 'json', toJsonCycle, MAX_CALL_STACK_MESSAGE);93 94 test('exports.sendMessage() validates payload', async t => {95 const subprocess = execa('ipc-echo-item.js', {ipc: true});96 await subprocess.sendMessage([undefined]);97 const {message} = await t.throwsAsync(subprocess);90- t.true(message.includes('sendMessage()\'s argument type is invalid'));98+ t.true(message.includes('Error: sendMessage()\'s argument type is invalid: the message cannot be serialized'));99+ t.true(message.includes(UNDEFINED_MESSAGE));100 });