Skip to content

Commit 730e511

Browse files
committed
child_process: better error reporting for exec
Report path to executable and argv on error, stderr is not enough in many cases. fix nodejs#6796
1 parent 4800310 commit 730e511

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

lib/child_process.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ exports.execFile = function(file /* args, options, callback */) {
664664
var exited = false;
665665
var timeoutId;
666666

667-
var ex;
667+
var ex = null;
668668

669669
function exithandler(code, signal) {
670670
if (exited) return;
@@ -689,16 +689,25 @@ exports.execFile = function(file /* args, options, callback */) {
689689
}
690690

691691
if (ex) {
692-
callback(ex, stdout, stderr);
692+
// Will be handled later
693693
} else if (code === 0 && signal === null) {
694694
callback(null, stdout, stderr);
695-
} else {
696-
ex = new Error('Command failed: ' + stderr);
695+
return;
696+
}
697+
698+
var cmd = file;
699+
if (args.length !== 0)
700+
cmd += ' ' + args.join(' ');
701+
702+
if (!ex) {
703+
ex = new Error('Command failed: ' + cmd + '\n' + stderr);
697704
ex.killed = child.killed || killed;
698705
ex.code = code < 0 ? uv.errname(code) : code;
699706
ex.signal = signal;
700-
callback(ex, stdout, stderr);
701707
}
708+
709+
ex.cmd = cmd;
710+
callback(ex, stdout, stderr);
702711
}
703712

704713
function errorhandler(e) {

test/simple/test-child-process-exec-error.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function test(fun, code) {
2828

2929
fun('does-not-exist', function(err) {
3030
assert.equal(err.code, code);
31+
assert(/does\-not\-exist/.test(err.cmd));
3132
errors++;
3233
});
3334

0 commit comments

Comments
 (0)