mirror of
https://gitlab.silvrtree.co.uk/martind2000/nci.git
synced 2025-01-10 23:45:07 +00:00
catch cmd errors at spawn + detail error message
This commit is contained in:
parent
c9cf204224
commit
2ecc354cb7
@ -23,6 +23,8 @@ Command.prototype.run = function(params, callback) {
|
|||||||
stdout = self.collectOut ? '' : null,
|
stdout = self.collectOut ? '' : null,
|
||||||
stderr = self.attachStderr ? '' : null;
|
stderr = self.attachStderr ? '' : null;
|
||||||
|
|
||||||
|
callback = _(callback).once();
|
||||||
|
|
||||||
if (!params.cmd) return callback(new Error('`cmd` is not set'));
|
if (!params.cmd) return callback(new Error('`cmd` is not set'));
|
||||||
if (!params.args) return callback(new Error('`args` is not set'));
|
if (!params.args) return callback(new Error('`args` is not set'));
|
||||||
params.options = params.options || {};
|
params.options = params.options || {};
|
||||||
@ -34,6 +36,16 @@ Command.prototype.run = function(params, callback) {
|
|||||||
self.emit('stdin', params.cmd + ' ' + params.args.join(' ') + '\n');
|
self.emit('stdin', params.cmd + ' ' + params.args.join(' ') + '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var extendError = function (err) {
|
||||||
|
err.message = (
|
||||||
|
'Error while spawn "' +
|
||||||
|
[params.cmd].concat(params.args || []).join(' ') + '": ' +
|
||||||
|
err.message
|
||||||
|
);
|
||||||
|
|
||||||
|
return err;
|
||||||
|
};
|
||||||
|
|
||||||
cmd.stdout.on('data', function(data) {
|
cmd.stdout.on('data', function(data) {
|
||||||
if (self.emitOut) self.emit('stdout', data);
|
if (self.emitOut) self.emit('stdout', data);
|
||||||
if (self.collectOut) stdout += data;
|
if (self.collectOut) stdout += data;
|
||||||
@ -47,11 +59,17 @@ Command.prototype.run = function(params, callback) {
|
|||||||
cmd.on('close', function(code) {
|
cmd.on('close', function(code) {
|
||||||
var err = null;
|
var err = null;
|
||||||
if (code !== 0) {
|
if (code !== 0) {
|
||||||
err = new Error('Spawned command exits with non-zero code: ' + code);
|
err = extendError(
|
||||||
|
new Error('Spawned command exits with non-zero code: ' + code)
|
||||||
|
);
|
||||||
err.stderr = stderr;
|
err.stderr = stderr;
|
||||||
}
|
}
|
||||||
callback(err, stdout);
|
callback(err, stdout);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
cmd.on('error', function(err) {
|
||||||
|
callback(extendError(err));
|
||||||
|
});
|
||||||
|
|
||||||
return cmd;
|
return cmd;
|
||||||
};
|
};
|
||||||
|
@ -44,7 +44,7 @@ describe('Shell command', function() {
|
|||||||
// messages and codes are slightly different across the OSes
|
// messages and codes are slightly different across the OSes
|
||||||
// e.g. at linux and macos
|
// e.g. at linux and macos
|
||||||
expect(err.message).match(
|
expect(err.message).match(
|
||||||
/^Spawned command exits with non-zero code: \d+/
|
/Spawned command exits with non-zero code: \d+/
|
||||||
);
|
);
|
||||||
expect(err.stderr).match(/echo1:.*not found/);
|
expect(err.stderr).match(/echo1:.*not found/);
|
||||||
expect(std.err).equal('');
|
expect(std.err).equal('');
|
||||||
|
Loading…
Reference in New Issue
Block a user