Improve error handling (#647)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 249e7c4 · Browse files · 3 files·+37 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+37 added−13 removed
- tests:+26 added−1 removed
- other:+11 added−12 removed
Provenance
- Commit
5b6d859- Tree
34345ee- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
249e7c4
3 files changed+37 added−13 removed
modified:lib/stream.js+11 added-12 removed
12234515 unmodified lines21222324252627282930313232333435363334353644 unmodified lines818283848586878988899092919293949516 unmodified lines112113114115117116117118119120121122123124125123124125126import {once} from 'node:events';import {setTimeout} from 'node:timers/promises';import {finished} from 'node:stream/promises';import process from 'node:process';import getStream, {getStreamAsBuffer, getStreamAsArrayBuffer} from 'get-stream';import mergeStreams from '@sindresorhus/merge-streams';15 unmodified lines return mergeStreams([spawned.stdout, spawned.stderr]);};
// On failure, `result.stdout|stderr|all` should contain the currently buffered stream// They are automatically closed and flushed by Node.js when the child process exits// We guarantee this by calling `childProcess.kill()`const getBufferedData = async (stream, streamPromise) => { // When `buffer` is `false`, `streamPromise` is `undefined` and there is no buffered data to retrieve if (!stream || streamPromise === undefined) { return; }
// Wait for the `all` stream to receive the last chunk before destroying the stream await setTimeout(0);
stream.destroy();
try { return await streamPromise; } catch (error) { return error.bufferedData;44 unmodified lines const [error] = await once(stream, 'error'); throw error;};
// The streams created by the `std*` options are automatically ended by `.pipe()`.// However `.pipe()` only does so when the source stream ended, not when it errored.// Therefore, when `childProcess.stdin|stdout|stderr` errors, those streams must be manually destroyed.const cleanupStdioStreams = stdioStreams => {const cleanupStdioStreams = (stdioStreams, error) => { for (const stdioStream of stdioStreams) { if (stdioStream.autoDestroy) { stdioStream.value.destroy(); stdioStream.value.destroy(error); } }};
16 unmodified lines allPromise, ...stdioStreams.map(stdioStream => waitForStreamEnd(stdioStream, processDone)), ]); } catch (error) { return Promise.all([ spawned.kill(); const results = await Promise.all([ {error, signal: error.signal, timedOut: error.timedOut}, getBufferedData(spawned.stdout, stdoutPromise), getBufferedData(spawned.stderr, stderrPromise), getBufferedData(spawned.all, allPromise), ]); } finally { cleanupStdioStreams(stdioStreams); spawned.kill(); cleanupStdioStreams(stdioStreams, error); return results; }};modified:test/helpers/generator.js+11 added
19 unmodified lines20212223242526272829303132333419 unmodified lines// eslint-disable-next-line require-yieldexport const throwingGenerator = function * () { throw new Error('generator error');};
export const infiniteGenerator = () => { const controller = new AbortController();
const generator = async function * () { yield 'foo'; await setTimeout(1e7, undefined, {signal: controller.signal}); };
return {iterable: generator(), abort: controller.abort.bind(controller)};};modified:test/stdio/iterable.js+15 added-1 removed
1+import {once} from 'node:events';2 import test from 'ava';3 import {execa, execaSync} from '../../index.js';4 import {setFixtureDir} from '../helpers/fixtures-dir.js';5 import {getStdinOption, getStdoutOption, getStderrOption, getStdioOption} from '../helpers/stdio.js';5-import {stringGenerator, binaryGenerator, asyncGenerator, throwingGenerator} from '../helpers/generator.js';6+import {stringGenerator, binaryGenerator, asyncGenerator, throwingGenerator, infiniteGenerator} from '../helpers/generator.js';7 8 setFixtureDir();9 10 const testIterable = async (t, stdioOption, fixtureName, getOptions) => {36 unmodified lines47 test('stdout option cannot be an iterable', testNoIterableOutput, getStdoutOption, execa);48 test('stderr option cannot be an iterable', testNoIterableOutput, getStderrOption, execa);49 test('stdout option cannot be an iterable - sync', testNoIterableOutput, getStdoutOption, execaSync);50 test('stderr option cannot be an iterable - sync', testNoIterableOutput, getStderrOption, execaSync);51+52+test('stdin option can be an infinite iterable', async t => {53+ const {iterable, abort} = infiniteGenerator();54+ try {55+ const childProcess = execa('stdin.js', getStdinOption(iterable));56+ const stdout = await once(childProcess.stdout, 'data');57+ t.is(stdout.toString(), 'foo');58+ childProcess.kill('SIGKILL');59+ await t.throwsAsync(childProcess, {message: /SIGKILL/});60+ } finally {61+ abort();62+ }63+});