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
Provenance
- Commit
bad2140- Tree
07bb069- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
0336882
4 files changed+56 added−39 removed
modified:index.d.ts+2 added
39 unmodified lines40 41 /**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.43 44+ If the spawned process fails, `error.stdout`, `error.stderr`, and `error.all` will contain the buffered data.45+46 @default true47 */48 readonly buffer?: boolean;49 373 unmodified linesmodified:index.js+31 added-30 removed
104 unmodified lines105 106 return mixed;107 }108 109+async function getBufferedData(stream, streamPromise) {110+ if (!stream) {111+ return;112+ }113+114+ stream.destroy();115+116+ try {117+ return await streamPromise;118+ } catch (error) {119+ return error.bufferedData;120+ }121+}122+123 function getStream(process, stream, {encoding, buffer, maxBuffer}) {124 if (!process[stream]) {125 return;126 }15 unmodified lines142 } else {143 ret = _getStream.buffer(process[stream], {maxBuffer});144 }145 132- return ret.catch(error => {133- error.stream = stream;134- error.message = `${stream} ${error.message}`;135- throw error;136- });146+ return ret;147 }148 149 function makeError(result, options) {150 const {stdout, stderr, signal} = result;21 unmodified lines172 if ('all' in result) {173 error.all = result.all;174 }175 176+ if ('bufferedData' in error) {177+ delete error.bufferedData;178+ }179+180 error.failed = true;181 error.timedOut = timedOut;182 error.isCanceled = isCanceled;183 error.killed = killed && !timedOut;154 unmodified lines338 });339 }340 }), cleanup);341 328- function destroy() {329- if (spawned.stdout) {330- spawned.stdout.destroy();331- }332-333- if (spawned.stderr) {334- spawned.stderr.destroy();335- }336-337- if (spawned.all) {338- spawned.all.destroy();339- }340- }341-342 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});346 347 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 }360 361 const [result, stdout, stderr, all] = results;362 result.stdout = handleOutput(parsed.options, stdout);30 unmodified lines393 killed: false394 };395 };396 395- // TODO: Use native "finally" syntax when targeting Node.js 10396- return pFinally(finalize(), destroy);397+ return finalize();398 };399 400 crossSpawn._enoent.hookChildProcess(spawned, parsed.parsed);401 108 unmodified linesmodified:readme.md+2 added
280 unmodified lines281 Default: `true`282 283 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.284 285+If the spawned process fails, [`error.stdout`](#stdout), [`error.stderr`](#stderr), and [`error.all`](#all) will contain the buffered data.286+287 #### input288 289 Type: `string | Buffer | stream.Readable`290 184 unmodified linesmodified:test.js+21 added-9 removed
63 unmodified lines64 t.is(stderr, undefined);65 t.is(all, undefined);66 });67 68+const WRONG_COMMAND = process.platform === 'win32' ?69+ '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' :70+ '';71+72 test('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 });78 79 test('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 });87 88 test('pass `stdout` to a file descriptor', async t => {170 unmodified lines259 const {stdout} = execa.sync('stdin', {input: Buffer.from('testing12', 'utf8')});260 t.is(stdout, 'testing12');261 });262 263+test('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+});268+269 test('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 });274 275 test('opts.stdout:ignore - stdout will not collect data', async t => {276 const {stdout} = await execa('stdin', {36 unmodified lines313 t.true(failed);314 });315 316 test('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 });322 323 test('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 });329 330 test('do not buffer stdout when `buffer` set to `false`', async t => {331 const promise = execa('max-buffer', ['stdout', '10'], {buffer: false});486 unmodified lines