From 048b23e08cd2f18f16ec4eed43e0dc535d402173 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Mon, 10 Jun 2019 21:22:31 -0700 Subject: [PATCH] Fix shape of exceptions thrown by `execa.sync()` (#277) --- index.js | 14 +++++++++++++- test.js | 8 ++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 0883ebe676..f7b4ef8ecc 100644 --- a/index.js +++ b/index.js @@ -415,7 +415,19 @@ module.exports.sync = (file, args, options) => { throw new TypeError('The `input` option cannot be a stream in sync mode'); } - const result = childProcess.spawnSync(parsed.file, parsed.args, parsed.options); + let result; + try { + result = childProcess.spawnSync(parsed.file, parsed.args, parsed.options); + } catch (error) { + throw makeError({error, stdout: '', stderr: '', all: ''}, { + joinedCommand, + parsed, + timedOut: false, + isCanceled: false, + killed: false + }); + } + result.stdout = handleOutput(parsed.options, result.stdout, result.error); result.stderr = handleOutput(parsed.options, result.stderr, result.error); diff --git a/test.js b/test.js index 5249888a11..a392ca2403 100644 --- a/test.js +++ b/test.js @@ -293,11 +293,11 @@ test('child_process.spawn() errors are propagated', async t => { t.is(exitCodeName, process.platform === 'win32' ? 'ENOTSUP' : 'EINVAL'); }); -test('child_process.spawnSync() errors are propagated', t => { - const {exitCodeName} = t.throws(() => { - execa.sync('noop', {uid: -1}); +test('child_process.spawnSync() errors are propagated with a correct shape', t => { + const {failed} = t.throws(() => { + execa.sync('noop', {timeout: -1}); }); - t.is(exitCodeName, process.platform === 'win32' ? 'ENOTSUP' : 'EINVAL'); + t.true(failed); }); test('maxBuffer affects stdout', async t => {