1

I am making a http request to a url which is returing a 500 error response(This is the expected behavior). But this is error is getting captured in the success function instead of error function.

$http.get("myUrl")
    .then(function (response) {
        console.log(response);
    }
    .function (error) {
        // Handle error here
    });

Please help in understanding this and the correct way to use this.

4
  • 1
    What library are you using? Shouldn't that be ).error(function()...)? Commented Sep 18, 2016 at 9:40
  • @Barmar "...then(...).error is not a function" this is the error I am getting now Commented Sep 18, 2016 at 9:51
  • What library is this? It's not jQuery. Is it Angular? Commented Sep 18, 2016 at 9:52
  • @Barmar Its angular Commented Sep 18, 2016 at 9:58

3 Answers 3

1

It should be either:

$http.get("myUrl")
    .then(function (response) {
        console.log(response);
    }
    ,function (error) {
        // Handle error here
    });

Or

$http.get("myUrl")
    .then(function (response) {
        console.log(response);
    })
    .catch (function(error) {
        // Handle error here
    });
Sign up to request clarification or add additional context in comments.

2 Comments

500 internal server error still comes in the first function
Is your REST api returning status as 500? Do you have any angular intercepter which handkes the Ajax calls?
1

If this is angulars $http, it's supposed to be something like this:

$http.get("myUrl")
  .then(
    function (response) {
      console.log(response);
    },
    function (error) {
      // Handle error here
    }
  );

You want two functions as your arguments to then(). The first is your successCallback, the second your errorCallback.

As an alternative you may add an catch() to your promise chain. Which is easier to read and prevents errors like yours.

Comments

0

I had an interceptor in my application which was causing the problem. All my error responses were intercepted and returned without a status. Below is the code.

return {
        'responseError': function(config) {

            if(config.status===401){
              //Do Something

              return config;
            }

          return config;
        }
    }

Changing the return statement to return $q.reject(config); started returning the correct status.

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.