Skip to content

Commit

Permalink
fix: handle code-less errors more carefully in exec
Browse files Browse the repository at this point in the history
If an error exists, but has no error code, it defaults to 1 (a common code for
most Unix commands). Tests have been omitted since this is an edge case that is
difficult to reproduce.

Fixes #536
  • Loading branch information
nfischer committed Nov 12, 2016
1 parent c6e4c40 commit b04afb1
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/exec.js
Expand Up @@ -77,22 +77,29 @@ function execSync(cmd, opts, pipe) {
"var child = require('child_process')",
" , fs = require('fs');",
'var childProcess = child.exec(' + JSON.stringify(cmd) + ', ' + optString + ', function(err) {',
' fs.writeFileSync(' + JSON.stringify(codeFile) + ", err ? err.code.toString() : '0');",
' var fname = ' + JSON.stringify(codeFile) + ';',
' if (!err) {',
' fs.writeFileSync(fname, "0");',
' } else if (err.code === undefined) {',
' fs.writeFileSync(fname, "1");',
' } else {',
' fs.writeFileSync(fname, err.code.toString());',
' }',
'});',
'var stdoutStream = fs.createWriteStream(' + JSON.stringify(stdoutFile) + ');',
'var stderrStream = fs.createWriteStream(' + JSON.stringify(stderrFile) + ');',
'childProcess.stdout.pipe(stdoutStream, {end: false});',
'childProcess.stderr.pipe(stderrStream, {end: false});',
'childProcess.stdout.pipe(process.stdout);',
'childProcess.stderr.pipe(process.stderr);'
'childProcess.stderr.pipe(process.stderr);',
].join('\n') +
(pipe ? '\nchildProcess.stdin.end(' + JSON.stringify(pipe) + ');\n' : '\n') +
[
'var stdoutEnded = false, stderrEnded = false;',
'function tryClosingStdout(){ if(stdoutEnded){ stdoutStream.end(); } }',
'function tryClosingStderr(){ if(stderrEnded){ stderrStream.end(); } }',
"childProcess.stdout.on('end', function(){ stdoutEnded = true; tryClosingStdout(); });",
"childProcess.stderr.on('end', function(){ stderrEnded = true; tryClosingStderr(); });"
"childProcess.stderr.on('end', function(){ stderrEnded = true; tryClosingStderr(); });",
].join('\n');

fs.writeFileSync(scriptFile, script);
Expand Down Expand Up @@ -121,8 +128,15 @@ function execSync(cmd, opts, pipe) {
"var child = require('child_process')",
" , fs = require('fs');",
'var childProcess = child.exec(' + JSON.stringify(cmd) + ', ' + optString + ', function(err) {',
' fs.writeFileSync(' + JSON.stringify(codeFile) + ", err ? err.code.toString() : '0');",
'});'
' var fname = ' + JSON.stringify(codeFile) + ';',
' if (!err) {',
' fs.writeFileSync(fname, "0");',
' } else if (err.code === undefined) {',
' fs.writeFileSync(fname, "1");',
' } else {',
' fs.writeFileSync(fname, err.code.toString());',
' }',
'});',
].join('\n') +
(pipe ? '\nchildProcess.stdin.end(' + JSON.stringify(pipe) + ');\n' : '\n');

Expand Down Expand Up @@ -177,7 +191,14 @@ function execAsync(cmd, opts, pipe, callback) {

var c = child.exec(cmd, opts, function (err) {
if (callback) {
callback(err ? err.code : 0, stdout, stderr);
if (!err) {
callback(0, stdout, stderr);
} else if (err.code === undefined) {
// See issue #536
callback(1, stdout, stderr);
} else {
callback(err.code, stdout, stderr);
}
}
});

Expand Down

0 comments on commit b04afb1

Please sign in to comment.