So I have a service defined as follows:
angular.module('services', ['ngResource'])
.factory('Things', function ($rootScope, $http) {
var basePath = 'rest/things/';
return {
getAll: function () {
return $http.post($rootScope.PAGES_URL + basePath + 'getAll/' + window.clientId, {});
}
};
});
Then, elsewhere, I'm consuming that service w/:
Things.getAll().success(function(things){
//do something w/ things
})
.error(function(err){
// Clearly, something went wrong w/ the request
});
What I'd like to do, is be able to "throw" the error condition if, for instance, there's a problem w/ the data at the service level. i.e.:
Data comes back as:
{
status:500,
message:'There was a problem w/ the data for this client'
}
And so then in the service there would be something like:
getAll: function () {
return $http.post($rootScope.PAGES_URL + basePath + 'getAll/' + window.clientId, {})
.throwError(function(data){
return (data.status && data.status == 200);
});
}
So when the throwError callback returns false, the error() promise would then be called instead of the success promise.
Does anyone have any ideas on how to accomplish this?
Thanks a bunch!
$httpcall. Inside the success, check thestatusproperty of the response; if it isn't in the200range, reject the deferred. Otherwise, resolve the deferred. Reject anything in the error callback. And return the deferred's promise from the service methodresponsecallback, check if the response's property,status, and if it's "bad", use$q.reject(response);, otherwisereturn response || $q.when(response);