Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3149e09

Browse files
committed
refactor: explicit handling for execa errors
This adds a dedicated if-condition to handle internal errors from execa (timeout, maxBuffer, etc.) and to distinguish these from "regular" errors (the command executed, but it failed with non-zero status).
1 parent 75d26de commit 3149e09

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

src/cmd.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,20 @@ function _cmd(options, command, commandArgs, userOptions) {
116116
stdout = '';
117117
stderr = "'" + command + "': command not found";
118118
code = COMMAND_NOT_FOUND_ERROR_CODE;
119-
} else {
119+
} else if (typeof result.stdout === 'string' &&
120+
typeof result.stderr === 'string' &&
121+
typeof result.code === 'number') {
122+
// Normal exit: execa was able to execute `command` and get a return value.
120123
stdout = result.stdout.toString();
121124
stderr = result.stderr.toString();
122125
code = result.code;
126+
} else {
127+
// Catch-all: execa tried to run `command` but it encountered some error
128+
// (ex. maxBuffer, timeout).
129+
stdout = result.stdout || '';
130+
stderr = result.stderr ||
131+
`'${command}' encountered an error during execution`;
132+
code = result.code > 0 ? result.code : 1;
123133
}
124134

125135
// Pass `continue: true` so we can specify a value for stdout.

test/cmd.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,26 @@ test('set cwd', t => {
178178
});
179179

180180
test('set maxBuffer (very small)', t => {
181-
const result = shell.cmd('shx', 'echo', '1234567890'); // default maxBuffer is ok
181+
let result = shell.cmd('shx', 'echo', '1234567890'); // default maxBuffer is ok
182182
t.falsy(shell.error());
183183
t.is(result.code, 0);
184184
t.is(result.stdout, '1234567890\n');
185-
shell.cmd('shx', 'echo', '1234567890', { maxBuffer: 6 });
185+
result = shell.cmd('shx', 'echo', '1234567890', { maxBuffer: 6 });
186186
t.truthy(shell.error());
187+
t.is(result.code, 1);
188+
t.is(result.stdout, '1234567890\n');
187189
});
188190

189191
test('set timeout option', t => {
190-
const result = shell.cmd(shell.config.execPath, 'test/resources/exec/slow.js', '100'); // default timeout is ok
192+
let result = shell.cmd(shell.config.execPath, 'test/resources/exec/slow.js', '100'); // default timeout is ok
191193
t.falsy(shell.error());
194+
t.is(result.stdout, 'fast\nslow\n');
192195
t.is(result.code, 0);
193-
shell.cmd(shell.config.execPath, 'test/resources/exec/slow.js', '2000', { timeout: 1000 }); // times out
196+
result = shell.cmd(shell.config.execPath, 'test/resources/exec/slow.js', '2000', { timeout: 1000 }); // times out
194197
t.truthy(shell.error());
198+
t.is(result.stdout, 'fast\n');
199+
t.truthy(result.stderr);
200+
t.is(result.code, 1);
195201
});
196202

197203
test('check process.env works', t => {

test/exec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,14 @@ test('set maxBuffer (very small)', t => {
152152
});
153153

154154
test('set timeout option', t => {
155-
const result = shell.exec(`${JSON.stringify(shell.config.execPath)} test/resources/exec/slow.js 100`); // default timeout is ok
155+
let result = shell.exec(`${JSON.stringify(shell.config.execPath)} test/resources/exec/slow.js 100`); // default timeout is ok
156156
t.falsy(shell.error());
157157
t.is(result.code, 0);
158-
shell.exec(`${JSON.stringify(shell.config.execPath)} test/resources/exec/slow.js 2000`, { timeout: 1000 }); // times out
158+
t.is(result.stdout, 'fast\nslow\n');
159+
result = shell.exec(`${JSON.stringify(shell.config.execPath)} test/resources/exec/slow.js 2000`, { timeout: 1000 }); // times out
159160
t.truthy(shell.error());
161+
t.is(result.code, 1);
162+
t.is(result.stdout, 'fast\n');
160163
});
161164

162165
test('check process.env works', t => {

test/resources/exec/slow.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env node
2+
console.log('fast');
23
setTimeout(function() {
34
console.log('slow');
45
}, parseInt(process.argv[2], 10));

0 commit comments

Comments
 (0)