Use rejected promise to handle errors without throwing

This commit is contained in:
Joe Fiorini
2015-06-04 17:09:33 -04:00
parent b0b5e3a726
commit f188ca79d6
2 changed files with 14 additions and 16 deletions

View File

@@ -26,7 +26,7 @@ function lodashAsPromised($q) {
return obj; return obj;
} }
function _reject(value, reason) { function _reject(reason) {
return $q.reject(reason); return $q.reject(reason);
} }

View File

@@ -93,34 +93,37 @@ function controller($rootScope,
var splitFacts = _.partition(facts, _.isEmpty); var splitFacts = _.partition(facts, _.isEmpty);
var emptyScans = splitFacts[0]; var emptyScans = splitFacts[0];
var nonEmptyScans = splitFacts[1]; var nonEmptyScans = splitFacts[1];
var result;
if (_.isEmpty(nonEmptyScans)) { if (_.isEmpty(nonEmptyScans)) {
// we have NO data, throw an error // we have NO data, throw an error
throw { result = _.reject({
name: 'NoScanData', name: 'NoScanData',
message: 'No scans ran on eithr of the dates you selected. Please try selecting different dates.', message: 'No scans ran on eithr of the dates you selected. Please try selecting different dates.',
dateValues: dateValues:
{ leftDate: $scope.leftDate.clone(), { leftDate: $scope.leftDate.clone(),
rightDate: $scope.rightDate.clone() rightDate: $scope.rightDate.clone()
} }
}; });
} else if (nonEmptyScans.length === 1) { } else if (nonEmptyScans.length === 1) {
// one of them is not empty, throw an error // one of them is not empty, throw an error
throw { result = _.reject({
name: 'InsufficientScanData', name: 'InsufficientScanData',
message: 'No scans ran on one of the selected dates. Please try selecting a different date.', message: 'No scans ran on one of the selected dates. Please try selecting a different date.',
dateValue: emptyScans[0].position === 'left' ? $scope.leftDate.clone() : $scope.rightDate.clone() dateValue: emptyScans[0].position === 'left' ? $scope.leftDate.clone() : $scope.rightDate.clone()
}; });
} else {
result = _.promise(facts);
} }
delete facts[0].position; delete facts[0].position;
delete facts[1].position; delete facts[1].position;
// all scans have data, rejoice! // all scans have data, rejoice!
return facts; return result;
}) })
.then(_.partial(compareFacts, _.log('activeModule', activeModule))) .then(_.partial(compareFacts, activeModule))
.then(function(info) { .then(function(info) {
// Clear out any errors from the previous run... // Clear out any errors from the previous run...
@@ -130,6 +133,8 @@ function controller($rootScope,
return info; return info;
}).catch(function(error) {
$scope.error = error;
}).finally(function() { }).finally(function() {
waitIndicator('stop'); waitIndicator('stop');
}); });
@@ -151,11 +156,7 @@ function controller($rootScope,
$location.search('module', newModuleName); $location.search('module', newModuleName);
reloadData({ module: newModule reloadData({ module: newModule
}, initialData) }, initialData).value();
.catch(function(error) {
$scope.error = error;
}).value();
}; };
function dateWatcher(dateProperty) { function dateWatcher(dateProperty) {
@@ -174,10 +175,7 @@ function controller($rootScope,
var params = {}; var params = {};
params[dateProperty] = newDate; params[dateProperty] = newDate;
reloadData(params) reloadData(params).value();
.catch(function(error) {
$scope.error = error;
}).value();
}; };
} }