Max Bündchen
Max Bündchen

Reputation: 1362

Angular $http not handling error

My angular app doesn't handle the errors in $http calls.

I have a $httpProvider where I return $q.reject(response) in errorResponse as required by documentation. In the console the angular just puts angular.min.js:99 GET http://localhost:8080/my/api 500 (Internal Server Error).

Code

console.log('before');
$http.get('/my/api', function(response){
   console.log('ok');
   console.log(response);
}, function(response){
    console.log('not ok');
    console.log(response)
});
console.log('after');

I just get 'before', 'after' and the message above.

Also in the network tab I get the expected response from server with status 500 and a json content in the body.

Except for the $httpProvider's errorResponse without a return $q.reject(), what could be the problem?

Upvotes: 1

Views: 428

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136144

You have syntactical mistake, you should use .then function and then put success & error callback to them.

Code

$http.get('/my/api').then(function(response){
   console.log('ok');
   console.log(response);
}, function(response){
    console.log('not ok');
    console.log(response)
});

What I understood by your code/question is, you are expecting to get error callback function to execute, but it is not doing that. If I missed something then do ask same in comments.

Upvotes: 5

Related Questions