I'm writing a function that handles results from an Angular $http. In my $http request I'm using .then() to handle results.
var GET = function (url) {
return $http({
method: 'GET',
url: url
}).then(
function (data) {
return data.data;
},
function (data) {
return data.data;
});
};
And then I call the GET function from another function, like this:
var initializeUsers = function () {
return dbService.GET(serverUrl + dbResource).then(
function (data) {
// Some code to handle success
},
function (data) {
// Some code to handle error
});
};
Here's the problem: If there is an error in the HTTP request, it is being handled by the error handler in the GET function. BUT, when the initializeUsers
is run, the error handler is NOT triggered, but rather the success handler. So the error does not "bubble" up, which is should
However, if I, instead of using .then
in the GET function, I use .success
and .error
, like this:
var GET = function (url) {
return $http({
method: 'GET',
url: url
}).success(
function (data) {
return data.data;
}).error(
function (data) {
return data.data;
});
};
it works fine in the initializeUsers
. HOWEVER, if I then call initializeUsers
from another function using .then
, the error handler in it is not triggered. I would seem that I have to use .success
and .error
all the way, which is not how it's supposed to work, as far as I can understand.
As long as I explicitly use .error
on every function in order to actually catch the error, otherwise the error gets lost and the next function acts as though the last one was successful.
I'm I misunderstanding, or is there something wrong with my code?