From a78de0195b59856ffd2aec13c9c0de029f2dc135 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 9 Feb 2015 15:40:43 -0300 Subject: [PATCH] check required arguments without throwing exceptions --- lib/server.js | 14 +++++++------- lib/utils.js | 19 ++++++++++--------- test/integration.js | 9 ++++----- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/server.js b/lib/server.js index 5030e23..2567273 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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 diff --git a/lib/utils.js b/lib/utils.js index aaaa456..905d798 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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,16 +17,17 @@ 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; }; /** - * + * * @desc rounds a JAvascript number * @param number * @return {number} diff --git a/test/integration.js b/test/integration.js index 75f430b..2279961 100644 --- a/test/integration.js +++ b/test/integration.js @@ -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) {