Skip to content
Myr

Improve error handling of streams (#271)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by Sindre Sorhus · parent 0336882 · Browse files · 4 files+56 added−39 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+56 added−39 removed

  • other:+56 added−39 removed

4 files+56 added−39 removed

modified:index.d.ts+2 added
39 unmodified lines
4041 /**42 Buffer the output from the spawned process. When buffering is disabled you must consume the output of the `stdout` and `stderr` streams because the promise will not be resolved/rejected until they have completed.4344 If the spawned process fails, `error.stdout`, `error.stderr`, and `error.all` will contain the buffered data.4546 @default true47 */48 readonly buffer?: boolean;49
373 unmodified lines
modified:index.js+31 added-30 removed
104 unmodified lines
105106 return mixed;107}108109async function getBufferedData(stream, streamPromise) {110 if (!stream) {111 return;112 }113114 stream.destroy();115116 try {117 return await streamPromise;118 } catch (error) {119 return error.bufferedData;120 }121}122123function getStream(process, stream, {encoding, buffer, maxBuffer}) {124 if (!process[stream]) {125 return;126 }
15 unmodified lines
142 } else {143 ret = _getStream.buffer(process[stream], {maxBuffer});144 }145132 return ret.catch(error => {133 error.stream = stream;134 error.message = `${stream} ${error.message}`;135 throw error;136 });146 return ret;147}148149function makeError(result, options) {150 const {stdout, stderr, signal} = result;
21 unmodified lines
172 if ('all' in result) {173 error.all = result.all;174 }175176 if ('bufferedData' in error) {177 delete error.bufferedData;178 }179180 error.failed = true;181 error.timedOut = timedOut;182 error.isCanceled = isCanceled;183 error.killed = killed && !timedOut;
154 unmodified lines
338 });339 }340 }), cleanup);341328 function destroy() {329 if (spawned.stdout) {330 spawned.stdout.destroy();331 }332333 if (spawned.stderr) {334 spawned.stderr.destroy();335 }336337 if (spawned.all) {338 spawned.all.destroy();339 }340 }341342 const handlePromise = () => {343 const processComplete = Promise.all([344 processDone,345 getStream(spawned, 'stdout', {encoding, buffer, maxBuffer}),346 getStream(spawned, 'stderr', {encoding, buffer, maxBuffer}),347 getStream(spawned, 'all', {encoding, buffer, maxBuffer: maxBuffer * 2})348 ]);343 const stdoutPromise = getStream(spawned, 'stdout', {encoding, buffer, maxBuffer});344 const stderrPromise = getStream(spawned, 'stderr', {encoding, buffer, maxBuffer});345 const allPromise = getStream(spawned, 'all', {encoding, buffer, maxBuffer: maxBuffer * 2});346347 const finalize = async () => {348 let results;349 try {353 results = await processComplete;350 results = await Promise.all([processDone, stdoutPromise, stderrPromise, allPromise]);351 } catch (error) {355 const {stream, code, signal} = error;356 results = [{error, stream, code, signal}, '', '', ''];352 const {code, signal} = error;353 results = await Promise.all([354 {error, code, signal},355 getBufferedData(spawned.stdout, stdoutPromise),356 getBufferedData(spawned.stderr, stderrPromise),357 getBufferedData(spawned.all, allPromise)358 ]);359 }360361 const [result, stdout, stderr, all] = results;362 result.stdout = handleOutput(parsed.options, stdout);
30 unmodified lines
393 killed: false394 };395 };396395 // TODO: Use native "finally" syntax when targeting Node.js 10396 return pFinally(finalize(), destroy);397 return finalize();398 };399400 crossSpawn._enoent.hookChildProcess(spawned, parsed.parsed);401
108 unmodified lines
modified:readme.md+2 added
280 unmodified lines
281Default: `true`282283Buffer the output from the spawned process. When buffering is disabled you must consume the output of the `stdout` and `stderr` streams because the promise will not be resolved/rejected until they have completed.284285If the spawned process fails, [`error.stdout`](#stdout), [`error.stderr`](#stderr), and [`error.all`](#all) will contain the buffered data.286287#### input288289Type: `string | Buffer | stream.Readable`290
184 unmodified lines
modified:test.js+21 added-9 removed
63 unmodified lines
64 t.is(stderr, undefined);65 t.is(all, undefined);66});6768const WRONG_COMMAND = process.platform === 'win32' ?69 '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' :70 '';7172test('stdout/stderr/all on process errors', async t => {73 const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command'));74 t.is(stdout, '');71 t.is(stderr, '');72 t.is(all, '');75 t.is(stderr, WRONG_COMMAND);76 t.is(all, WRONG_COMMAND);77});7879test('stdout/stderr/all on process errors, in sync mode', t => {80 const {stdout, stderr, all} = t.throws(() => {81 execa.sync('wrong command');82 });83 t.is(stdout, '');80 t.is(stderr, process.platform === 'win32' ?81 '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' :82 '');84 t.is(stderr, WRONG_COMMAND);85 t.is(all, undefined);86});8788test('pass `stdout` to a file descriptor', async t => {
170 unmodified lines
259 const {stdout} = execa.sync('stdin', {input: Buffer.from('testing12', 'utf8')});260 t.is(stdout, 'testing12');261});262263test('stdin errors are handled', async t => {264 const child = execa('noop');265 child.stdin.emit('error', new Error('test'));266 await t.throwsAsync(child, /test/);267});268269test('child process errors are handled', async t => {270 const child = execa('noop');271 child.emit('error', new Error('test'));264 await t.throwsAsync(child, /Command failed.*\ntest/);272 await t.throwsAsync(child, /test/);273});274275test('opts.stdout:ignore - stdout will not collect data', async t => {276 const {stdout} = await execa('stdin', {
36 unmodified lines
313 t.true(failed);314});315316test('maxBuffer affects stdout', async t => {309 await t.throwsAsync(execa('max-buffer', ['stdout', '11'], {maxBuffer: 10}), /stdout maxBuffer exceeded/);317 await t.notThrowsAsync(execa('max-buffer', ['stdout', '10'], {maxBuffer: 10}));318 const {stdout, all} = await t.throwsAsync(execa('max-buffer', ['stdout', '11'], {maxBuffer: 10}), /max-buffer stdout/);319 t.is(stdout, '.'.repeat(10));320 t.is(all, '.'.repeat(10));321});322323test('maxBuffer affects stderr', async t => {314 await t.throwsAsync(execa('max-buffer', ['stderr', '13'], {maxBuffer: 12}), /stderr maxBuffer exceeded/);315 await t.notThrowsAsync(execa('max-buffer', ['stderr', '12'], {maxBuffer: 12}));324 await t.notThrowsAsync(execa('max-buffer', ['stderr', '10'], {maxBuffer: 10}));325 const {stderr, all} = await t.throwsAsync(execa('max-buffer', ['stderr', '11'], {maxBuffer: 10}), /max-buffer stderr/);326 t.is(stderr, '.'.repeat(10));327 t.is(all, '.'.repeat(10));328});329330test('do not buffer stdout when `buffer` set to `false`', async t => {331 const promise = execa('max-buffer', ['stdout', '10'], {buffer: false});
486 unmodified lines