handle errors properly

This commit is contained in:
Jean-Baptiste Dominguez 2018-09-05 17:23:09 +09:00
commit 2d6a1528c1
2 changed files with 73 additions and 41 deletions

View file

@ -2,7 +2,7 @@
angular.module('copayApp.controllers').controller('amountController', amountController); 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; var vm = this;
vm.allowSend = false; vm.allowSend = false;
@ -94,10 +94,19 @@ function amountController(configService, $filter, gettextCatalog, $ionicModal, $
vm.toWallet = profileService.getWallet(vm.toWalletId); vm.toWallet = profileService.getWallet(vm.toWalletId);
ongoingProcess.set('connectingShapeshift', true); ongoingProcess.set('connectingShapeshift', true);
shapeshiftService.getMarketData(vm.fromWallet.coin, vm.toWallet.coin, function(data) { shapeshiftService.getMarketData(vm.fromWallet.coin, vm.toWallet.coin, function(err, data) {
vm.thirdParty.data['minAmount'] = vm.minAmount = parseFloat(data.minimum);
vm.thirdParty.data['maxAmount'] = vm.maxAmount = parseFloat(data.maxLimit); if (err) {
ongoingProcess.set('connectingShapeshift', false); // 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);
}
}); });
} }
} }

View file

@ -28,16 +28,32 @@ angular
return service; 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) { function getMarketData(coinIn, coinOut, cb) {
service.coinIn = coinIn; service.coinIn = coinIn;
service.coinOut = coinOut; service.coinOut = coinOut;
shapeshiftApiService shapeshiftApiService
.marketInfo(service.coinIn, service.coinOut) .marketInfo(service.coinIn, service.coinOut)
.then(function (marketData) { .then(function (response) {
service.marketData = marketData; if (!response || response.error) {
service.rateString = service.marketData.rate.toString() + ' ' + coinOut.toUpperCase() + '/' + coinIn.toUpperCase(); handleError(response, 'Invalid response', cb);
if (cb) { } else {
cb(marketData); 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) { function shiftIt(coinIn, coinOut, withdrawalAddress, returnAddress, amount, cb) {
// Test if the amount is correct depending on the min and max // 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) { if (!amount || typeof amount !== 'number' || amount < service.marketData.minimum || amount > service.marketData.maxLimit) {
var err = new Error('Invalid amount'); cb(new Error('Invalid amount'));
cb(err);
} else { } else {
// Init service data // Init service data
service.withdrawalAddress = withdrawalAddress; service.withdrawalAddress = withdrawalAddress;
@ -56,36 +71,44 @@ angular
service.amount = amount; service.amount = amount;
// Check the address // Check the address
shapeshiftApiService.ValidateAddress(withdrawalAddress, coinOut).then(function onSuccess(data) { shapeshiftApiService
if (data && data.isvalid) { .ValidateAddress(withdrawalAddress, coinOut)
.then(function onSuccess(response) {
if (response && response.isvalid) {
// Prepare the transaction shapeshift side // Prepare the transaction shapeshift side
shapeshiftApiService.NormalTx(service).then(function onResponse(data) { shapeshiftApiService.NormalTx(service).then(function onResponse(response) {
var txData = data; // If error, return it
if (!response || response.error) {
// If the content is not that it was expected, get back an error handleError(response, 'Invalid response', cb);
if (!txData || !txData.orderId || !txData.deposit) { } else {
cb(new Error('Invalid response')); var txData = response;
} else {
// Get back the data // If the content is not that it was expected, get back an error
service.depositInfo = txData; if (!txData || !txData.orderId || !txData.deposit) {
var shapeshiftData = { if (cb) {
coinIn: coinIn, cb(new Error('Invalid response'));
coinOut: coinOut, }
toWalletId: service.toWalletId, } else {
minAmount: service.marketData.minimum, // Get back the data
maxAmount: service.marketData.maxLimit, service.depositInfo = txData;
orderId: txData.orderId, var shapeshiftData = {
toAddress: txData.deposit coinIn: coinIn,
}; coinOut: coinOut,
cb(null, shapeshiftData); toWalletId: service.toWalletId,
} minAmount: service.marketData.minimum,
}); maxAmount: service.marketData.maxLimit,
} else { orderId: txData.orderId,
var err = new Error('Invalid address or coin'); toAddress: txData.deposit
cb(err); };
} cb(null, shapeshiftData);
}); }
}
});
} else if (cb) {
cb(new Error('Invalid address or coin'));
}
});
} }
} }
} }