Child process errors should reject the promise right away (#270)
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 2307d95 · Browse files · 2 files·+23 added−31 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+23 added−31 removed
- other:+23 added−31 removed
Provenance
- Commit
3b8df50- Tree
2212ffc- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
2307d95
2 files changed+23 added−31 removed
modified:index.js+12 added-24 removed
260 unmodified lines261 spawned.kill(parsed.options.killSignal);262 }, parsed.options.timeout);263 }264 265- const resolvable = (() => {266- let extracted;267- const promise = new Promise(resolve => {268- extracted = resolve;269- });270- promise.resolve = extracted;271- return promise;272- })();273-265 // TODO: Use native "finally" syntax when targeting Node.js 10275- const processDone = pFinally(new Promise(resolve => {266+ const processDone = pFinally(new Promise((resolve, reject) => {267 spawned.on('exit', (code, signal) => {268 if (timedOut) {278- resolvable.resolve([279- {code, signal}, '', '', ''280- ]);269+ return reject(Object.assign(new Error('Timed out'), {code, signal}));270 }271 272 resolve({code, signal});273 });274 275 spawned.on('error', error => {287- resolve({error});276+ reject(error);277 });278 279 if (spawned.stdin) {280 spawned.stdin.on('error', error => {292- resolve({error});281+ reject(error);282 });283 }284 }), cleanup);285 11 unmodified lines297 }298 }299 300 const handlePromise = () => {312- let processComplete = Promise.all([301+ const processComplete = Promise.all([302 processDone,303 getStream(spawned, 'stdout', {encoding, buffer, maxBuffer}),304 getStream(spawned, 'stderr', {encoding, buffer, maxBuffer}),305 getStream(spawned, 'all', {encoding, buffer, maxBuffer: maxBuffer * 2})306 ]);307 319- if (timeoutId) {320- processComplete = Promise.race([321- processComplete,322- resolvable323- ]);324- }325-308 const finalize = async () => {309+ let results;310+ try {327- const results = await processComplete;311+ results = await processComplete;312+ } catch (error) {313+ const {stream, code, signal} = error;314+ results = [{error, stream, code, signal}, '', '', ''];315+ }316 317 const [result, stdout, stderr, all] = results;318 result.stdout = handleOutput(parsed.options, stdout);319 result.stderr = handleOutput(parsed.options, stderr);135 unmodified linesmodified:test.js+11 added-7 removed
63 unmodified lines64 t.is(stderr, undefined);65 t.is(all, undefined);66 });67 68-const WRONG_COMMAND_STDERR = process.platform === 'win32' ?69- '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' :70- '';71-68 test('stdout/stderr/all on process errors', async t => {69 const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command'));70 t.is(stdout, '');75- t.is(stderr, WRONG_COMMAND_STDERR);76- t.is(all, WRONG_COMMAND_STDERR);71+ t.is(stderr, '');72+ t.is(all, '');73 });74 75 test('stdout/stderr/all on process errors, in sync mode', t => {76 const {stdout, stderr, all} = t.throws(() => {77 execa.sync('wrong command');78 });79 t.is(stdout, '');84- t.is(stderr, WRONG_COMMAND_STDERR);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+ '');83 t.is(all, undefined);84 });85 86 test('pass `stdout` to a file descriptor', async t => {127 unmodified lines214 /The `input` option cannot be a stream in sync mode/215 );216 });217 218+test('child process errors rejects promise right away', async t => {219+ const child = execa('forever');220+ child.emit('error', new Error('test'));221+ await t.throwsAsync(child, /test/);222+});223+224 test('execa() returns a promise with kill() and pid', t => {225 const {kill, pid} = execa('noop', ['foo']);226 t.is(typeof kill, 'function');227 t.is(typeof pid, 'number');512 unmodified lines