From 2d6a1528c10fd5f02ff7170ba34731cf15b44f93 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Wed, 5 Sep 2018 17:23:09 +0900 Subject: [PATCH] handle errors properly --- src/js/controllers/amount.js | 19 ++++-- src/js/services/shapeshift.service.js | 95 +++++++++++++++++---------- 2 files changed, 73 insertions(+), 41 deletions(-) diff --git a/src/js/controllers/amount.js b/src/js/controllers/amount.js index c24c567ed..c1cd9a118 100644 --- a/src/js/controllers/amount.js +++ b/src/js/controllers/amount.js @@ -2,7 +2,7 @@ angular.module('copayApp.controllers').controller('amountController', amountController); -function amountController(configService, $filter, gettextCatalog, $ionicModal, $ionicScrollDelegate, lodash, $log, nodeWebkitService, rateService, $scope, $state, $timeout, sendFlowService, shapeshiftService, txFormatService, platformInfo, profileService, walletService, $window, ongoingProcess) { +function amountController(configService, $filter, gettextCatalog, $ionicModal, $ionicScrollDelegate, lodash, $log, nodeWebkitService, rateService, $scope, $state, $timeout, sendFlowService, shapeshiftService, txFormatService, platformInfo, profileService, walletService, $window, ongoingProcess, popupService) { var vm = this; vm.allowSend = false; @@ -94,10 +94,19 @@ function amountController(configService, $filter, gettextCatalog, $ionicModal, $ vm.toWallet = profileService.getWallet(vm.toWalletId); ongoingProcess.set('connectingShapeshift', true); - shapeshiftService.getMarketData(vm.fromWallet.coin, vm.toWallet.coin, function(data) { - vm.thirdParty.data['minAmount'] = vm.minAmount = parseFloat(data.minimum); - vm.thirdParty.data['maxAmount'] = vm.maxAmount = parseFloat(data.maxLimit); - ongoingProcess.set('connectingShapeshift', false); + shapeshiftService.getMarketData(vm.fromWallet.coin, vm.toWallet.coin, function(err, data) { + + if (err) { + // Error stop here + ongoingProcess.set('connectingShapeshift', false); + popupService.showAlert(gettextCatalog.getString('Shapeshift Error'), err.toString(), function () { + $ionicHistory.goBack(); + }); + } else { + vm.thirdParty.data['minAmount'] = vm.minAmount = parseFloat(data.minimum); + vm.thirdParty.data['maxAmount'] = vm.maxAmount = parseFloat(data.maxLimit); + ongoingProcess.set('connectingShapeshift', false); + } }); } } diff --git a/src/js/services/shapeshift.service.js b/src/js/services/shapeshift.service.js index c290f1fc3..21a5368fe 100644 --- a/src/js/services/shapeshift.service.js +++ b/src/js/services/shapeshift.service.js @@ -28,16 +28,32 @@ angular return service; + function handleError(response, defaultMessage, cb) { + if (!response || !response.error || !response.error.message) { + if (cb) { + cb(new Error(defaultMessage)); + } + } else { + if (cb) { + cb(new Error(response.error.message)); + } + } + } + function getMarketData(coinIn, coinOut, cb) { service.coinIn = coinIn; service.coinOut = coinOut; shapeshiftApiService .marketInfo(service.coinIn, service.coinOut) - .then(function (marketData) { - service.marketData = marketData; - service.rateString = service.marketData.rate.toString() + ' ' + coinOut.toUpperCase() + '/' + coinIn.toUpperCase(); - if (cb) { - cb(marketData); + .then(function (response) { + if (!response || response.error) { + handleError(response, 'Invalid response', cb); + } else { + service.marketData = response; + service.rateString = service.marketData.rate.toString() + ' ' + coinOut.toUpperCase() + '/' + coinIn.toUpperCase(); + if (cb) { + cb(null, response); + } } }); } @@ -45,8 +61,7 @@ angular function shiftIt(coinIn, coinOut, withdrawalAddress, returnAddress, amount, cb) { // Test if the amount is correct depending on the min and max if (!amount || typeof amount !== 'number' || amount < service.marketData.minimum || amount > service.marketData.maxLimit) { - var err = new Error('Invalid amount'); - cb(err); + cb(new Error('Invalid amount')); } else { // Init service data service.withdrawalAddress = withdrawalAddress; @@ -56,36 +71,44 @@ angular service.amount = amount; // Check the address - shapeshiftApiService.ValidateAddress(withdrawalAddress, coinOut).then(function onSuccess(data) { - if (data && data.isvalid) { + shapeshiftApiService + .ValidateAddress(withdrawalAddress, coinOut) + .then(function onSuccess(response) { + if (response && response.isvalid) { - // Prepare the transaction shapeshift side - shapeshiftApiService.NormalTx(service).then(function onResponse(data) { - var txData = data; - - // If the content is not that it was expected, get back an error - if (!txData || !txData.orderId || !txData.deposit) { - cb(new Error('Invalid response')); - } else { - // Get back the data - service.depositInfo = txData; - var shapeshiftData = { - coinIn: coinIn, - coinOut: coinOut, - toWalletId: service.toWalletId, - minAmount: service.marketData.minimum, - maxAmount: service.marketData.maxLimit, - orderId: txData.orderId, - toAddress: txData.deposit - }; - cb(null, shapeshiftData); - } - }); - } else { - var err = new Error('Invalid address or coin'); - cb(err); - } - }); + // Prepare the transaction shapeshift side + shapeshiftApiService.NormalTx(service).then(function onResponse(response) { + // If error, return it + if (!response || response.error) { + handleError(response, 'Invalid response', cb); + } else { + var txData = response; + + // If the content is not that it was expected, get back an error + if (!txData || !txData.orderId || !txData.deposit) { + if (cb) { + cb(new Error('Invalid response')); + } + } else { + // Get back the data + service.depositInfo = txData; + var shapeshiftData = { + coinIn: coinIn, + coinOut: coinOut, + toWalletId: service.toWalletId, + minAmount: service.marketData.minimum, + maxAmount: service.marketData.maxLimit, + orderId: txData.orderId, + toAddress: txData.deposit + }; + cb(null, shapeshiftData); + } + } + }); + } else if (cb) { + cb(new Error('Invalid address or coin')); + } + }); } } }