I have created a service with the following definition:
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, {});
}
};
});
Now, in another part of my code, I am using this service as follows:
Things.getAll().success(function(things){
//do something with the things
})
.error(function(err){
// An error occurred during the request
});
What I want to achieve is the ability to handle an error condition if there is a problem with the data at the service level. For example:
Suppose the data returned is:
{
status:500,
message:'There was a problem with the data for this client'
}
In that case, I would like to implement something like this in the service:
getAll: function () {
return $http.post($rootScope.PAGES_URL + basePath + 'getAll/' + window.clientId, {})
.throwError(function(data){
return (data.status && data.status == 200);
});
}
With this implementation, when the throwError callback returns false, the error() promise will be triggered instead of the success promise.
Does anyone have suggestions on how to make this work?
Thank you!