Skip to content

Commit

Permalink
Fix shape of exceptions thrown by execa.sync() (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored and sindresorhus committed Jun 11, 2019
1 parent 50a924a commit 048b23e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 13 additions & 1 deletion index.js
Expand Up @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions test.js
Expand Up @@ -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 => {
Expand Down

0 comments on commit 048b23e

Please sign in to comment.