Browse Source

refactor errors

activeAddress
Ivan Socolsky 10 years ago
parent
commit
8e7fa61466
  1. 40
      lib/server.js
  2. 16
      test/integration.js

40
lib/server.js

@ -23,6 +23,16 @@ var Address = require('./model/address');
var TxProposal = require('./model/txproposal'); var TxProposal = require('./model/txproposal');
function CopayError(code, message, inner) {
this.code = code;
this.message = message;
this.inner = inner;
};
function BadRequestError(message) {
this.message = message || 'Bad request';
};
/** /**
* Creates an instance of the Copay server. * Creates an instance of the Copay server.
* @constructor * @constructor
@ -57,9 +67,9 @@ CopayServer.prototype.createWallet = function(opts, cb) {
pubKey; pubKey;
Utils.checkRequired(opts, ['id', 'name', 'm', 'n', 'pubKey']); Utils.checkRequired(opts, ['id', 'name', 'm', 'n', 'pubKey']);
if (!Wallet.verifyCopayerLimits(opts.m, opts.n)) return cb('Invalid m/n combination'); if (!Wallet.verifyCopayerLimits(opts.m, opts.n)) return cb(new BadRequestError('Invalid combination of required copayers / total copayers'));
var network = opts.network || 'livenet'; var network = opts.network || 'livenet';
if (network != 'livenet' && network != 'testnet') return cb('Invalid network'); if (network != 'livenet' && network != 'testnet') return cb(new BadRequestError('Invalid network'));
try { try {
pubKey = new PublicKey.fromString(opts.pubKey); pubKey = new PublicKey.fromString(opts.pubKey);
@ -69,7 +79,7 @@ CopayServer.prototype.createWallet = function(opts, cb) {
self.storage.fetchWallet(opts.id, function(err, wallet) { self.storage.fetchWallet(opts.id, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
if (wallet) return cb('Wallet already exists'); if (wallet) return cb(new CopayError('WEXISTS', 'Wallet already exists'));
var wallet = new Wallet({ var wallet = new Wallet({
id: opts.id, id: opts.id,
@ -95,7 +105,7 @@ CopayServer.prototype.getWallet = function(opts, cb) {
self.storage.fetchWallet(opts.id, function(err, wallet) { self.storage.fetchWallet(opts.id, function(err, wallet) {
if (err) return cb(err); if (err) return cb(err);
if (!wallet) return cb('Wallet not found'); if (!wallet) return cb(new BadRequestError('Wallet not found'));
return cb(null, wallet); return cb(null, wallet);
}); });
}; };
@ -132,13 +142,13 @@ CopayServer.prototype.joinWallet = function(opts, cb) {
if (err) return cb(err); if (err) return cb(err);
if (!self._verifySignature(opts.xPubKey, opts.xPubKeySignature, wallet.pubKey)) { if (!self._verifySignature(opts.xPubKey, opts.xPubKeySignature, wallet.pubKey)) {
return cb('Bad request'); return cb(new BadRequestError());
} }
if (_.find(wallet.copayers, { if (_.find(wallet.copayers, {
xPubKey: opts.xPubKey xPubKey: opts.xPubKey
})) return cb('Copayer already in wallet'); })) return cb(new CopayError('CINWALLET', 'Copayer already in wallet'));
if (wallet.copayers.length == wallet.n) return cb('Wallet full'); if (wallet.copayers.length == wallet.n) return cb(new CopayError('WFULL', 'Wallet full'));
var copayer = new Copayer({ var copayer = new Copayer({
id: opts.id, id: opts.id,
@ -234,7 +244,7 @@ CopayServer.prototype.verifyMessageSignature = function(opts, cb) {
if (err) return cb(err); if (err) return cb(err);
var copayer = wallet.getCopayer(opts.copayerId); var copayer = wallet.getCopayer(opts.copayerId);
if (!copayer) return cb('Copayer not found'); if (!copayer) return cb(new BadRequestError('Copayer not found'));
var isValid = self._verifySignature(opts.message, opts.signature, copayer.signingPubKey); var isValid = self._verifySignature(opts.message, opts.signature, copayer.signingPubKey);
return cb(null, isValid); return cb(null, isValid);
@ -268,7 +278,7 @@ CopayServer.prototype._getUtxos = function(opts, cb) {
// Get addresses for this wallet // Get addresses for this wallet
self.storage.fetchAddresses(opts.walletId, function(err, addresses) { self.storage.fetchAddresses(opts.walletId, function(err, addresses) {
if (err) return cb(err); if (err) return cb(err);
if (addresses.length == 0) return cb('The wallet has no addresses'); if (addresses.length == 0) return cb(new BadRequestError('The wallet has no addresses'));
var addresses = _.pluck(addresses, 'address'); var addresses = _.pluck(addresses, 'address');
@ -453,12 +463,12 @@ CopayServer.prototype.signTx = function(opts, cb) {
self.fetchTx(opts.walletId, opts.txProposalId, function(err, txp) { self.fetchTx(opts.walletId, opts.txProposalId, function(err, txp) {
if (err) return cb(err); if (err) return cb(err);
if (!txp) return cb('Transaction proposal not found'); if (!txp) return cb(new BadRequestError('Transaction proposal not found'));
var action = _.find(txp.actions, { var action = _.find(txp.actions, {
copayerId: opts.copayerId copayerId: opts.copayerId
}); });
if (action) return cb('Copayer already voted on this transaction proposal'); if (action) return cb(new CopayError('CVOTED', 'Copayer already voted on this transaction proposal'));
if (txp.status != 'pending') return cb('The transaction proposal is not pending'); if (txp.status != 'pending') return cb(new CopayError('TXNOTPENDING', 'The transaction proposal is not pending'));
txp.sign(opts.copayerId, opts.signature); txp.sign(opts.copayerId, opts.signature);
@ -495,12 +505,12 @@ CopayServer.prototype.rejectTx = function(opts, cb) {
self.fetchTx(opts.walletId, opts.txProposalId, function(err, txp) { self.fetchTx(opts.walletId, opts.txProposalId, function(err, txp) {
if (err) return cb(err); if (err) return cb(err);
if (!txp) return cb('Transaction proposal not found'); if (!txp) return cb(new BadRequestError('Transaction proposal not found'));
var action = _.find(txp.actions, { var action = _.find(txp.actions, {
copayerId: opts.copayerId copayerId: opts.copayerId
}); });
if (action) return cb('Copayer already voted on this transaction proposal'); if (action) return cb(new CopayError('CVOTED', 'Copayer already voted on this transaction proposal'));
if (txp.status != 'pending') return cb('The transaction proposal is not pending'); if (txp.status != 'pending') return cb(new CopayError('TXNOTPENDING', 'The transaction proposal is not pending'));
txp.reject(opts.copayerId); txp.reject(opts.copayerId);

16
test/integration.js

@ -218,7 +218,7 @@ describe('Copay server', function() {
id: '345' id: '345'
}, function(err, wallet) { }, function(err, wallet) {
should.exist(err); should.exist(err);
err.should.equal('Wallet not found'); err.message.should.equal('Wallet not found');
done(); done();
}); });
}); });
@ -306,7 +306,7 @@ describe('Copay server', function() {
opts.n = pair.n; opts.n = pair.n;
server.createWallet(opts, function(err) { server.createWallet(opts, function(err) {
should.exist(err); should.exist(err);
err.should.contain('Invalid m/n combination'); err.message.should.equal('Invalid combination of required copayers / total copayers');
return cb(); return cb();
}); });
}, function(err) { }, function(err) {
@ -413,7 +413,8 @@ describe('Copay server', function() {
wallet.status.should.equal('complete'); wallet.status.should.equal('complete');
server.joinWallet(copayer2Opts, function(err) { server.joinWallet(copayer2Opts, function(err) {
should.exist(err); should.exist(err);
err.should.equal('Wallet full'); err.code.should.equal('WFULL');
err.message.should.equal('Wallet full');
done(); done();
}); });
}); });
@ -442,7 +443,8 @@ describe('Copay server', function() {
should.not.exist(err); should.not.exist(err);
server.joinWallet(copayerOpts, function(err) { server.joinWallet(copayerOpts, function(err) {
should.exist(err); should.exist(err);
err.should.equal('Copayer already in wallet'); err.code.should.equal('CINWALLET');
err.message.should.equal('Copayer already in wallet');
done(); done();
}); });
}); });
@ -468,7 +470,7 @@ describe('Copay server', function() {
xPubKeySignature: 'bad sign', xPubKeySignature: 'bad sign',
}; };
server.joinWallet(copayerOpts, function(err) { server.joinWallet(copayerOpts, function(err) {
err.should.contain('Bad request'); err.message.should.equal('Bad request');
done(); done();
}); });
}); });
@ -518,7 +520,7 @@ describe('Copay server', function() {
xPubKeySignature: someXPubKeysSignatures[0], xPubKeySignature: someXPubKeysSignatures[0],
}; };
server.joinWallet(copayerOpts, function(err) { server.joinWallet(copayerOpts, function(err) {
err.should.contain('Bad request'); err.message.should.equal('Bad request');
done(); done();
}); });
}); });
@ -571,7 +573,7 @@ describe('Copay server', function() {
signature: 'dummy', signature: 'dummy',
}; };
server.verifyMessageSignature(opts, function(err, isValid) { server.verifyMessageSignature(opts, function(err, isValid) {
err.should.equal('Copayer not found'); err.message.should.equal('Copayer not found');
done(); done();
}); });
}); });

Loading…
Cancel
Save