replace all errors with ClientError

This commit is contained in:
Ivan Socolsky 2015-02-04 13:31:02 -03:00
parent 283eca4e12
commit b5e089bd18
2 changed files with 38 additions and 26 deletions

21
lib/clienterror.js Normal file
View File

@ -0,0 +1,21 @@
function ClientError() {
var args = Array.prototype.slice.call(arguments);
switch (args.length) {
case 0:
this.code = 'BADREQUEST';
this.message = 'Bad request';
break;
case 1:
this.code = 'BADREQUEST';
this.message = args[0];
break;
default:
case 2:
this.code = args[0];
this.message = args[1];
break;
}
};
module.exports = ClientError;

View File

@ -13,6 +13,7 @@ var PublicKey = Bitcore.PublicKey;
var HDPublicKey = Bitcore.HDPublicKey;
var Explorers = require('bitcore-explorers');
var ClientError = require('./clienterror');
var Utils = require('./utils');
var Storage = require('./storage');
var SignUtils = require('./signutils');
@ -23,16 +24,6 @@ var Address = require('./model/address');
var TxProposal = require('./model/txproposal');
function CopayError(code, message, inner) {
this.code = code;
this.message = message;
this.inner = inner;
};
function RequestError(message) {
this.message = message || 'Bad request';
};
/**
* Creates an instance of the Copay server.
* @constructor
@ -67,9 +58,9 @@ CopayServer.prototype.createWallet = function(opts, cb) {
pubKey;
Utils.checkRequired(opts, ['id', 'name', 'm', 'n', 'pubKey']);
if (!Wallet.verifyCopayerLimits(opts.m, opts.n)) return cb(new RequestError('Invalid combination of required copayers / total copayers'));
if (!Wallet.verifyCopayerLimits(opts.m, opts.n)) return cb(new ClientError('Invalid combination of required copayers / total copayers'));
var network = opts.network || 'livenet';
if (network != 'livenet' && network != 'testnet') return cb(new RequestError('Invalid network'));
if (network != 'livenet' && network != 'testnet') return cb(new ClientError('Invalid network'));
try {
pubKey = new PublicKey.fromString(opts.pubKey);
@ -79,7 +70,7 @@ CopayServer.prototype.createWallet = function(opts, cb) {
self.storage.fetchWallet(opts.id, function(err, wallet) {
if (err) return cb(err);
if (wallet) return cb(new CopayError('WEXISTS', 'Wallet already exists'));
if (wallet) return cb(new ClientError('WEXISTS', 'Wallet already exists'));
var wallet = new Wallet({
id: opts.id,
@ -105,7 +96,7 @@ CopayServer.prototype.getWallet = function(opts, cb) {
self.storage.fetchWallet(opts.id, function(err, wallet) {
if (err) return cb(err);
if (!wallet) return cb(new RequestError('Wallet not found'));
if (!wallet) return cb(new ClientError('Wallet not found'));
return cb(null, wallet);
});
};
@ -142,13 +133,13 @@ CopayServer.prototype.joinWallet = function(opts, cb) {
if (err) return cb(err);
if (!self._verifySignature(opts.xPubKey, opts.xPubKeySignature, wallet.pubKey)) {
return cb(new RequestError());
return cb(new ClientError());
}
if (_.find(wallet.copayers, {
xPubKey: opts.xPubKey
})) return cb(new CopayError('CINWALLET', 'Copayer already in wallet'));
if (wallet.copayers.length == wallet.n) return cb(new CopayError('WFULL', 'Wallet full'));
})) return cb(new ClientError('CINWALLET', 'Copayer already in wallet'));
if (wallet.copayers.length == wallet.n) return cb(new ClientError('WFULL', 'Wallet full'));
var copayer = new Copayer({
id: opts.id,
@ -244,7 +235,7 @@ CopayServer.prototype.verifyMessageSignature = function(opts, cb) {
if (err) return cb(err);
var copayer = wallet.getCopayer(opts.copayerId);
if (!copayer) return cb(new RequestError('Copayer not found'));
if (!copayer) return cb(new ClientError('Copayer not found'));
var isValid = self._verifySignature(opts.message, opts.signature, copayer.signingPubKey);
return cb(null, isValid);
@ -278,7 +269,7 @@ CopayServer.prototype._getUtxos = function(opts, cb) {
// Get addresses for this wallet
self.storage.fetchAddresses(opts.walletId, function(err, addresses) {
if (err) return cb(err);
if (addresses.length == 0) return cb(new RequestError('The wallet has no addresses'));
if (addresses.length == 0) return cb(new ClientError('The wallet has no addresses'));
var addresses = _.pluck(addresses, 'address');
@ -428,7 +419,7 @@ CopayServer.prototype.createTx = function(opts, cb) {
txp.inputs = self._selectUtxos(txp, utxos);
if (!txp.inputs) {
return cb(new CopayError('INSUFFICIENTFUNDS', 'Insufficient funds'));
return cb(new ClientError('INSUFFICIENTFUNDS', 'Insufficient funds'));
}
// no need to do this now: // TODO remove this comment
@ -464,12 +455,12 @@ CopayServer.prototype.signTx = function(opts, cb) {
self.fetchTx(opts.walletId, opts.txProposalId, function(err, txp) {
if (err) return cb(err);
if (!txp) return cb(new RequestError('Transaction proposal not found'));
if (!txp) return cb(new ClientError('Transaction proposal not found'));
var action = _.find(txp.actions, {
copayerId: opts.copayerId
});
if (action) return cb(new CopayError('CVOTED', 'Copayer already voted on this transaction proposal'));
if (txp.status != 'pending') return cb(new CopayError('TXNOTPENDING', 'The transaction proposal is not pending'));
if (action) return cb(new ClientError('CVOTED', 'Copayer already voted on this transaction proposal'));
if (txp.status != 'pending') return cb(new ClientError('TXNOTPENDING', 'The transaction proposal is not pending'));
txp.sign(opts.copayerId, opts.signature);
@ -506,12 +497,12 @@ CopayServer.prototype.rejectTx = function(opts, cb) {
self.fetchTx(opts.walletId, opts.txProposalId, function(err, txp) {
if (err) return cb(err);
if (!txp) return cb(new RequestError('Transaction proposal not found'));
if (!txp) return cb(new ClientError('Transaction proposal not found'));
var action = _.find(txp.actions, {
copayerId: opts.copayerId
});
if (action) return cb(new CopayError('CVOTED', 'Copayer already voted on this transaction proposal'));
if (txp.status != 'pending') return cb(new CopayError('TXNOTPENDING', 'The transaction proposal is not pending'));
if (action) return cb(new ClientError('CVOTED', 'Copayer already voted on this transaction proposal'));
if (txp.status != 'pending') return cb(new ClientError('TXNOTPENDING', 'The transaction proposal is not pending'));
txp.reject(opts.copayerId);