1

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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.