diff --git a/lib/job.js b/lib/job.js index 25b9028..f51aeae 100644 --- a/lib/job.js +++ b/lib/job.js @@ -191,12 +191,9 @@ Job.prototype = { this.config.spawn(cmd.command, cmd.args, options, function (err, proc) { if (err) { - if (err === 127) { - return next(127, '', 'Command not found'); - } - - console.log('fail!', err, cmd.command, cmd.args); - return next(500, '', 'Unable to execude command :( ' + err); + // Shell exit code 1 represents general/unknown error + var msg = err.message || err.toString() || 'Unknown Error'; + return next(1, '', 'Unable to execute command: ' + msg); } proc.stdout.setEncoding('utf8'); @@ -218,7 +215,12 @@ Job.prototype = { std.err += buf; }); - proc.on('exit', function(exitCode) { + var finished = false; + var done = function (exitCode) { + // Guard against double calls. + if (finished) return; + finished = true; + var end = new Date(); var elapsed = end.getTime() - start.getTime(); @@ -245,10 +247,15 @@ Job.prototype = { } next(exitCode, std.out, std.err); - }); + }; + + proc.on('exit', done); - proc.on('error', function() { - // prevent node from throwing an error + proc.on('error', function(err) { + // Add err message to STDOUT + std.err = std.err + '\n' + (err.message || err.toString() || 'Unknown Error'); + // Shell exit code 1 represents general/unknown error + done(1); }); var strCmd = shellQuote([cmd.command].concat(cmd.args)); diff --git a/lib/spawn-normal.js b/lib/spawn-normal.js index 8533c0a..62f7280 100644 --- a/lib/spawn-normal.js +++ b/lib/spawn-normal.js @@ -1,10 +1,15 @@ var spawn = require('child_process').spawn +var _ = require('lodash') module.exports = function (command, args, options, done) { + // Prevent https://github.com/joyent/node/issues/9158 + options = _.extend({}, options) + + // Trap any other synchronous errors try { done(null, spawn(command, args, options)) - } catch (e) { - done(127) + } catch (err) { + done(err) } }