check required arguments without throwing exceptions

This commit is contained in:
Ivan Socolsky 2015-02-09 15:40:43 -03:00
parent 8f277b179c
commit a78de0195b
3 changed files with 21 additions and 21 deletions

View File

@ -58,7 +58,7 @@ CopayServer.initialize = function(opts) {
*/
CopayServer.getInstanceWithAuth = function(opts, cb) {
Utils.checkRequired(opts, ['copayerId', 'message', 'signature']);
if (!Utils.checkRequired(opts, ['copayerId', 'message', 'signature'])) return cb(new ClientError('Required argument missing'));
var server = new CopayServer();
server.storage.fetchCopayerLookup(opts.copayerId, function(err, copayer) {
@ -89,7 +89,7 @@ CopayServer.prototype.createWallet = function(opts, cb) {
var self = this,
pubKey;
Utils.checkRequired(opts, ['name', 'm', 'n', 'pubKey']);
if (!Utils.checkRequired(opts, ['name', 'm', 'n', 'pubKey'])) return cb(new ClientError('Required argument missing'));
if (_.isEmpty(opts.name)) return cb(new ClientError('Invalid wallet name'));
if (!Wallet.verifyCopayerLimits(opts.m, opts.n))
@ -155,7 +155,7 @@ CopayServer.prototype._verifySignature = function(text, signature, pubKey) {
CopayServer.prototype.joinWallet = function(opts, cb) {
var self = this;
Utils.checkRequired(opts, ['walletId', 'name', 'xPubKey', 'xPubKeySignature']);
if (!Utils.checkRequired(opts, ['walletId', 'name', 'xPubKey', 'xPubKeySignature'])) return cb(new ClientError('Required argument missing'));
if (_.isEmpty(opts.name)) return cb(new ClientError('Invalid copayer name'));
@ -237,7 +237,7 @@ CopayServer.prototype.getAddresses = function(opts, cb) {
CopayServer.prototype.verifyMessageSignature = function(opts, cb) {
var self = this;
Utils.checkRequired(opts, ['message', 'signature']);
if (!Utils.checkRequired(opts, ['message', 'signature'])) return cb(new ClientError('Required argument missing'));
self.getWallet({}, function(err, wallet) {
if (err) return cb(err);
@ -389,7 +389,7 @@ CopayServer.prototype._selectUtxos = function(txp, utxos) {
CopayServer.prototype.createTx = function(opts, cb) {
var self = this;
Utils.checkRequired(opts, ['toAddress', 'amount']);
if (!Utils.checkRequired(opts, ['toAddress', 'amount'])) return cb(new ClientError('Required argument missing'));
Utils.runLocked(self.walletId, cb, function(cb) {
self.getWallet({}, function(err, wallet) {
@ -477,7 +477,7 @@ CopayServer.prototype._broadcastTx = function(txp, cb) {
CopayServer.prototype.signTx = function(opts, cb) {
var self = this;
Utils.checkRequired(opts, ['txProposalId', 'signatures']);
if (!Utils.checkRequired(opts, ['txProposalId', 'signatures'])) return cb(new ClientError('Required argument missing'));
self.getWallet({}, function(err, wallet) {
if (err) return cb(err);
@ -533,7 +533,7 @@ CopayServer.prototype.signTx = function(opts, cb) {
CopayServer.prototype.rejectTx = function(opts, cb) {
var self = this;
Utils.checkRequired(opts, ['txProposalId']);
if (!Utils.checkRequired(opts, ['txProposalId'])) return cb(new ClientError('Required argument missing'));
self.getTx({
id: opts.txProposalId

View File

@ -4,11 +4,11 @@ var Lock = require('./lock');
var Utils = {};
Utils.runLocked = function (token, cb, task) {
Utils.runLocked = function(token, cb, task) {
var self = this;
Lock.get(token, function (lock) {
var _cb = function () {
Lock.get(token, function(lock) {
var _cb = function() {
cb.apply(null, arguments);
lock.free();
};
@ -17,12 +17,13 @@ Utils.runLocked = function (token, cb, task) {
};
Utils.checkRequired = function (obj, args) {
Utils.checkRequired = function(obj, args) {
args = [].concat(args);
if (!_.isObject(obj)) throw 'Required arguments missing';
_.each(args, function (arg) {
if (!obj.hasOwnProperty(arg)) throw "Missing required argument '" + arg + "'";
});
if (!_.isObject(obj)) return false;
for (var i = 0; i < args.length; i++) {
if (!obj.hasOwnProperty(args[i])) return false;
}
return true;
};
/**

View File

@ -427,12 +427,11 @@ describe('Copay server', function() {
name: 'me',
xPubKey: someXPubKeys[0],
};
try {
server.joinWallet(copayerOpts, function(err) {});
} catch (e) {
e.should.contain('xPubKeySignature');
server.joinWallet(copayerOpts, function(err) {
err.should.exist;
err.message.should.contain('argument missing');
done();
}
});
});
it('should fail to join with wrong signature', function(done) {