How to handle a single 404 in AngularJS's $q
angularjs, promise
Solution
Is there a way to "reraise" in $q?
Yes, you can rethrow by returning a rejected promise from the handler:
return $q.reject(new Error("Re Thrown")); // this is an actual `throw` in most
// promise implemenentations
In case an `$http` call 404 is not an error, you can recover from it. One of the cool features of promises is that we get to recover from errors:
var makeCallAndRecover(url){
return $http.get(...).catch(function(err){
// recover here if err is 404
if(err.status === 404) return null; //returning recovery
// otherwise return a $q.reject
return $q.reject(err);
});
}
Problem
I have a couple chained $http combined with a single $http using $q.all([prmiseA, promiseB]). Everything is working fine, I get the data back and errors are handled no problem. Except that on occasion data won't be found on a particular http call and it is not an error. I am using a service to separate the logic from the UI. And my call looks like this ``` $scope.Loading = true; var p = Service.Call(Param1, Param2); p.then(function () { $scope.Loading = false; }, function (reason) { $scope.Loading = false; $scope.alerts.push({ msg: "Error loading information " + Param1, type: "danger" }); }) ``` What I would like to be able to do is handling the 404 on that one URL inside the 'Service.Call' function. So that the UI code above remains untouched. My problem is that if I add an error handler to the specific call that may return a 404. Then all errors are "handled" and so I loose errors for that one call. Is there a way to "reraise" in $q?