Fix shape of exceptions rejected by `execa()` (#276)
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 bad2140 · Browse files · 2 files·+54 added−16 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+54 added−16 removed
- other:+54 added−16 removed
Provenance
- Commit
49c74ac- Tree
44bb2bf- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
bad2140
2 files changed+54 added−16 removed
modified:index.js+34 added-16 removed
226 unmodified lines227 228 return [file, ...args].join(' ');229 }230 231+function mergePromiseProperty(spawned, getPromise, property) {232+ Object.defineProperty(spawned, property, {233+ value(...args) {234+ return getPromise()[property](...args);235+ },236+ writable: true,237+ enumerable: false,238+ configurable: true239+ });240+}241+242+// The return value is a mixin of `childProcess` and `Promise`243+function mergePromise(spawned, getPromise) {244+ mergePromiseProperty(spawned, getPromise, 'then');245+ mergePromiseProperty(spawned, getPromise, 'catch');246+247+ // TODO: Remove the `if`-guard when targeting Node.js 10248+ if (Promise.prototype.finally) {249+ mergePromiseProperty(spawned, getPromise, 'finally');250+ }251+252+ return spawned;253+}254+255 function spawnedKill(kill, signal = 'SIGTERM', options = {}) {256 const killResult = kill(signal);257 setKillTimeout(kill, signal, options, killResult);258 return killResult;39 unmodified lines298 let spawned;299 try {300 spawned = childProcess.spawn(parsed.file, parsed.args, parsed.options);301 } catch (error) {302+ return mergePromise(new childProcess.ChildProcess(), () =>278- return Promise.reject(makeError({error, stdout: '', stderr: '', all: ''}, {279- command,280- parsed,281- timedOut: false,282- isCanceled: false,283- killed: false284- }));303+ Promise.reject(makeError({error, stdout: '', stderr: '', all: ''}, {304+ command,305+ parsed,306+ timedOut: false,307+ isCanceled: false,308+ killed: false309+ }))310+ );311 }312 313 const kill = spawned.kill.bind(spawned);314 spawned.kill = spawnedKill.bind(null, kill);113 unmodified lines428 handleInput(spawned, parsed.options.input);429 430 spawned.all = makeAllStream(spawned);431 406- // eslint-disable-next-line promise/prefer-await-to-then407- spawned.then = (onFulfilled, onRejected) => handlePromise().then(onFulfilled, onRejected);408- spawned.catch = onRejected => handlePromise().catch(onRejected);432 spawned.cancel = () => {433 if (spawned.kill()) {434 isCanceled = true;435 }436 };437 415- // TODO: Remove the `if`-guard when targeting Node.js 10416- if (Promise.prototype.finally) {417- spawned.finally = onFinally => handlePromise().finally(onFinally);418- }419-420- return spawned;438+ return mergePromise(spawned, handlePromise);439 };440 441 module.exports = execa;442 85 unmodified linesmodified:test.js+20 added
299 unmodified lines300 t.is(typeof kill, 'function');301 t.is(typeof pid, 'number');302 });303 304+test('child_process.spawn() propagated errors have correct shape', t => {305+ const cp = execa('noop', {uid: -1});306+ t.notThrows(() => {307+ cp.catch(() => {});308+ cp.unref();309+ cp.on('error', () => {});310+ });311+});312+313 test('child_process.spawn() errors are propagated', async t => {314 const {failed} = await t.throwsAsync(execa('noop', {uid: -1}));315 t.true(failed);316 });355 unmodified lines672 const included = ee.listeners('exit').includes(listener);673 t.false(included);674 });675 676+test('promise methods are not enumerable', t => {677+ const descriptors = Object.getOwnPropertyDescriptors(execa('noop'));678+ // eslint-disable-next-line promise/prefer-await-to-then679+ t.false(descriptors.then.enumerable);680+ t.false(descriptors.catch.enumerable);681+ // TOOD: Remove the `if`-guard when targeting Node.js 10682+ if (Promise.prototype.finally) {683+ t.false(descriptors.finally.enumerable);684+ }685+});686+687 // TOOD: Remove the `if`-guard when targeting Node.js 10688 if (Promise.prototype.finally) {689 test('finally function is executed on success', async t => {690 let isCalled = false;147 unmodified lines