I have implemented a function in a service:
function retrieveData(link) {
return $http.get(link).then(function (res) {
return res.info;
});
}
If the request is successful, everything works smoothly - I receive a promise as expected.
However, if I provide a URL that triggers a 404 error, I encounter the following:
TypeError: Cannot read property 'info' of undefined
This error occurs on the line: return res.info;
Upon inspecting the Chrome Developer Tools, I can confirm that the GET
request returns a 404 status code.
Why is Angular (v1.4.7, Tried v1.5.0 as well) passing undefined
to my successCallback in case of an error?
(My intention is to handle failures in the calling code.)
Edit: @jcaron provided a helpful hint. The problem seems to stem from this configuration line (created by another developer):
$httpProvider.interceptors.push('ErrorInterceptor');
There appears to be a flaw in the design of the interceptor:
function ErrorInterceptor($q, $window) {
return {
response: function (response) {
return response;
},
responseError: function (response) {
if (response.status === 500) {
$window.location.href = '/error';
return $q.reject(response);
}
}
};