From cef93325f4485650f8deaeb5bb281456008601d8 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 8 Sep 2014 16:37:33 -0300 Subject: [PATCH] Catch error when creating new TXP --- js/controllers/send.js | 8 ++++++++ js/models/core/Wallet.js | 2 +- test/mocks/FakeWallet.js | 2 +- test/test.Wallet.js | 13 +++++++++++++ test/unit/controllers/controllersSpec.js | 12 ++++++++++++ 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/js/controllers/send.js b/js/controllers/send.js index 552d556c8..a857df175 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -117,6 +117,14 @@ angular.module('copayApp.controllers').controller('SendController', var w = $rootScope.wallet; function done(err, ntxid, merchantData) { + if (err) { + var message = 'The transaction' + (w.isShared() ? ' proposal' : '') + ' could not be created'; + notification.error('Error', message); + $scope.loading = false; + $scope.loadTxs(); + return; + } + // If user is granted the privilege of choosing // their own amount, add it to the tx. if (merchantData && +merchantData.total === 0) { diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index a8441e8aa..5952fbad1 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -2045,7 +2045,7 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, comment, opts, cb) } this.getUnspent(function(err, safeUnspent) { - if (err) return cb(err); + if (err) return cb(new Error('Could not get list of UTXOs')); var ntxid = self.createTxSync(toAddress, amountSatStr, comment, safeUnspent, opts); if (!ntxid) { diff --git a/test/mocks/FakeWallet.js b/test/mocks/FakeWallet.js index 77b2d04a5..ee2621a1e 100644 --- a/test/mocks/FakeWallet.js +++ b/test/mocks/FakeWallet.js @@ -46,7 +46,7 @@ var FakeWallet = function() { FakeWallet.prototype.createTx = function(toAddress, amountSatStr, comment, opts, cb) { var callback = cb || opts; - callback({}); + callback(null, {}); } FakeWallet.prototype.sendTx = function(ntxid, cb) { diff --git a/test/test.Wallet.js b/test/test.Wallet.js index be685ebf5..38edc1709 100644 --- a/test/test.Wallet.js +++ b/test/test.Wallet.js @@ -792,6 +792,19 @@ describe('Wallet model', function() { }); }); + describe('#createTx', function () { + it('should fail if insight server is down', function (done) { + var w = cachedCreateW2(); + var utxo = createUTXO(w); + w.blockchain.fixUnspent(utxo); + sinon.stub(w, 'getUnspent').yields('error', null); + w.createTx(toAddress, amountSatStr, null, function(err, ntxid) { + chai.expect(err.message).to.equal('Could not get list of UTXOs'); + done(); + }); + }); + }); + describe('#createTxSync', function() { it('should fail if amount below min value', function() { var w = cachedCreateW2(); diff --git a/test/unit/controllers/controllersSpec.js b/test/unit/controllers/controllersSpec.js index b99da8e3c..dee2feb1b 100644 --- a/test/unit/controllers/controllersSpec.js +++ b/test/unit/controllers/controllersSpec.js @@ -285,6 +285,18 @@ describe("Unit: Controllers", function() { sinon.assert.callCount(scope.loadTxs, 1); }); + it('should not send txp when there is an error at creation', function() { + sendForm.address.$setViewValue('mkfTyEk7tfgV611Z4ESwDDSZwhsZdbMpVy'); + sendForm.amount.$setViewValue(1000); + scope.wallet.totalCopayers = scope.wallet.requiredCopayers = 1; + sinon.stub(scope.wallet, 'createTx').yields('error'); + var spySendTx = sinon.spy(scope.wallet, 'sendTx'); + scope.loadTxs = sinon.spy(); + + scope.submitForm(sendForm); + sinon.assert.callCount(spySendTx, 0); + sinon.assert.callCount(scope.loadTxs, 1); + }); }); describe("Unit: Version Controller", function() {