From 8313d9fa90a585bfca4b057f8c733b53c7f63bf7 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 9 Nov 2016 00:41:17 -0500 Subject: [PATCH] Better error handling for imports. Check for bad CSV data on lastpass (when no header row is included). --- src/Web/wwwroot/app/services/importService.js | 14 ++++++- .../app/tools/toolsImportController.js | 37 ++++++++++++++++--- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/Web/wwwroot/app/services/importService.js b/src/Web/wwwroot/app/services/importService.js index 6e43a7a20d..1fee585905 100644 --- a/src/Web/wwwroot/app/services/importService.js +++ b/src/Web/wwwroot/app/services/importService.js @@ -172,7 +172,8 @@ function parseData(data) { var folders = [], sites = [], - siteRelationships = []; + siteRelationships = [], + badDataSites = 0; angular.forEach(data, function (value, key) { var folderIndex = folders.length, @@ -190,6 +191,10 @@ } } + if ((!value.name || value.name === '') && (!value.password || value.password === '')) { + badDataSites++; + } + sites.push({ favorite: value.fav === '1', uri: value.url && value.url !== '' ? trimUri(value.url) : null, @@ -214,7 +219,12 @@ } }); - success(folders, sites, siteRelationships); + if (badDataSites > (data.length / 2)) { + error('CSV data is not formatted correctly from LastPass. Please check your import file and try again.'); + } + else { + success(folders, sites, siteRelationships); + } } } diff --git a/src/Web/wwwroot/app/tools/toolsImportController.js b/src/Web/wwwroot/app/tools/toolsImportController.js index fe04be776c..14f67d1845 100644 --- a/src/Web/wwwroot/app/tools/toolsImportController.js +++ b/src/Web/wwwroot/app/tools/toolsImportController.js @@ -31,15 +31,40 @@ }, importError); } - function importError(errorMessage) { + function importError(error) { $analytics.eventTrack('Import Data Failed', { label: $scope.model.source }); $uibModalInstance.dismiss('cancel'); - if (errorMessage) { - toastr.error(errorMessage); - } - else { - toastr.error('Something went wrong. Try again.', 'Oh No!'); + + if (error) { + var data = error.data; + if (data && data.ValidationErrors) { + var message = ''; + for (var key in data.ValidationErrors) { + if (!data.ValidationErrors.hasOwnProperty(key)) { + continue; + } + + for (var i = 0; i < data.ValidationErrors[key].length; i++) { + message += (key + ': ' + data.ValidationErrors[key][i] + ' '); + } + } + + if (message !== '') { + toastr.error(message); + return; + } + } + else if (data && data.Message) { + toastr.error(data.Message); + return; + } + else { + toastr.error(error); + return; + } } + + toastr.error('Something went wrong. Try again.', 'Oh No!'); } $scope.close = function () {