Permalink
Browse files

Fail on rejected promise returned from Opt.val() and Arg.val() when u…

…sing commands through API (Close #24)
  • Loading branch information...
1 parent 1ac9132 commit d26dffcff3e924254818ef145cce43177afed8b3 @arikon arikon committed Oct 19, 2012
Showing with 27 additions and 26 deletions.
  1. +16 −12 lib/cmd.js
  2. +11 −14 src/cmd.coffee
View
28 lib/cmd.js
@@ -383,7 +383,7 @@ exports.Cmd = Cmd = (function() {
};
Cmd.prototype._processParams = function(params, desc) {
- var i, n, notExists, v, vals, _i, _j, _len, _len1;
+ var i, n, notExists, res, v, vals, _i, _j, _len, _len1;
notExists = [];
for (_i = 0, _len = desc.length; _i < _len; _i++) {
i = desc[_i];
@@ -399,7 +399,9 @@ exports.Cmd = Cmd = (function() {
}
for (_j = 0, _len1 = vals.length; _j < _len1; _j++) {
v = vals[_j];
- i._saveVal(params, v);
+ if (Q.isRejected(res = i._saveVal(params, v))) {
+ return res;
+ }
}
}
return this._setDefaults(params, notExists);
@@ -493,16 +495,18 @@ exports.Cmd = Cmd = (function() {
if (p.argv.length) {
return _this.reject("Unknown command: " + cmds.join(' '));
}
- return Q.fail(_this._do({
- cmd: p.cmd,
- opts: _this._processParams(opts, _this._opts),
- args: _this._processParams(args, _this._args)
- }), function(res) {
- if (res && res.exitCode === 0) {
- return res.toString();
- } else {
- return _this.reject(res);
- }
+ return Q.all([_this._processParams(opts, _this._opts), _this._processParams(args, _this._args)]).spread(function(opts, args) {
+ return _this._do({
+ cmd: p.cmd,
+ opts: opts,
+ args: args
+ }).fail(function(res) {
+ if (res && res.exitCode === 0) {
+ return res.toString();
+ } else {
+ return _this.reject(res);
+ }
+ });
});
});
};
View
25 src/cmd.coffee
@@ -305,7 +305,8 @@ exports.Cmd = class Cmd
vals = [vals]
for v in vals
- i._saveVal(params, v)
+ if Q.isRejected(res = i._saveVal(params, v))
+ return res
# set defaults
@_setDefaults params, notExists
@@ -366,19 +367,15 @@ exports.Cmd = class Cmd
if p.argv.length
return @reject "Unknown command: " + cmds.join ' '
- # catch fails from .only() options
- Q.fail(
- @_do({
- cmd: p.cmd,
- opts: @_processParams(opts, @_opts),
- args: @_processParams(args, @_args)
- }),
- (res) =>
- if res and res.exitCode is 0
- res.toString()
- else
- @reject(res)
- )
+ Q.all([@_processParams(opts, @_opts), @_processParams(args, @_args)])
+ .spread (opts, args) =>
+ @_do({ cmd: p.cmd, opts: opts, args: args })
+ # catch fails from .only() options
+ .fail (res) =>
+ if res and res.exitCode is 0
+ res.toString()
+ else
+ @reject(res)
###*
Return reject of actions results promise with error code.

0 comments on commit d26dffc

Please sign in to comment.