Angular.js: $http Service Not Calling Error Callback on 400

Created on 8 May 2013  ·  26Comments  ·  Source: angular/angular.js

Version: 1.1.4

createNewWall: function(wall, successCallback, errorCallback) {
    return $http.post($rootScope.baseAppsAPI + '/walls', null, {
       params: wall
    }).success(function(data, status) {
       return successCallback.call(this, data);
    }).error(function(data) {
       return errorCallback.call(this, data);
    });
}

I verified that the angular isSuccess function is working correctly. I did this by adding it to the window object and verifying that the below code (placed in my sucess callback ) resulted in the error callback being called.

if (window.isSuccess(status)) {
   return successCallback.call(this, data);
} else {
  return errorCallback.call(this, data);
}

Any idea as to what is causing the issue? Something going wrong with the $http promises not being correctly rejected/resolved?

Most helpful comment

For me @lsiv568's anwser did the trick. It's kind of weird at first but you have to return a rejected promise manually when you build an error interceptor (source).

.factory('myInterceptor', function($q) {
  return {
    response: function (response) { return response; },
    responseError: function(response) {
      // do stuff or
      return $q.reject(response);
    }
  };
});

All 26 comments

I am facing the same problem. The way you closed the issue may mean that I can be doing something wrong etc. Can you please share your solution? Can't find this anywhere.

Robert.,

I closed the issue because the problem went away (miraculously) after I
made a few modifications to my service. However, it appears to come back
intermittently. So, I no longer know if it is a problem on my end or a
true problem with angular. If you want, I can reopen the issue and you can
add your problem to it? Does that work for yoU?

On Mon, May 13, 2013 at 9:13 AM, Robert [email protected] wrote:

I am facing the same problem. The way you closed the issue may mean that I
can be doing something wrong etc. Can you please share your solution? Can't
find this anywhere.


Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/2609#issuecomment-17810417
.

Respectfully,

Louis W. Sivillo

As stated above, I am reopening this issue because it happens intermittently. rtpm, please include your use case/any additional info that may help identify the issue

Robert,
Thanks for sending this along.

I had an http interceptor that i wrote for basic auth that was not rejecting the promise on response errors.

Thanks bot of you guys. I had the same problem.

+1 happens to me when server returns 401

+1
Callback function not working while server returns 401 status code.

Any progress on this? Facing the same problem with a 400.

For me @lsiv568's anwser did the trick. It's kind of weird at first but you have to return a rejected promise manually when you build an error interceptor (source).

.factory('myInterceptor', function($q) {
  return {
    response: function (response) { return response; },
    responseError: function(response) {
      // do stuff or
      return $q.reject(response);
    }
  };
});

Facing the same problem with a 400..
But why is the issue closed, is there any specific resolution for this issue?

I'm also facing this problem with error 400. Was this issue resolved? If so, in which version?

I'm encountering the same problem with all >299 errors. I'm using Restangular with a custom response interceptor, but I'm getting the error with the out-of-the-box $http service, with the exception of a CSRF token header. (I tried rejecting the deferred object from within Restangular manually anyway, but as expected that didn't change anything.)

I am encountering this issue as well, can we reopen this?

The problem reported in the orignal issue was due to a custom interceptor not handling error conditions properly (a promise wasn't rejected). If someone still encounters an issue it is most probably the same situation - please verify that you don't have buggy custom interceptors. If you still believe that something is not quite right on the AngularJS side please provide a _minimal, live_ reproduce scenario (ideally using http://plnkr.co/ but any other similar tool will do).

@pkozlowski-opensource Thanks, I can indeed confirm that was my issue (d'oh).

There should be a note in the Angular docs regarding this - I had no idea i was completely overriding $http promise behaviour implementing my interceptor.

I also ran into this problem and had an interceptor which I looked at again and again because it did indeed have the correct return. I finally found the culprit. If you are using multiple calls to success/error or then you actually have to ensure they return the correct thing too :) success callbacks should return the modified response while error callbacks should return a rejection with $q.reject just like the interceptor.

+1 thanks guys. I had the same issue with global customization of the $http.interceptor

+1 for @chmanie 's answer. Thank you very much! Fixed our problem.

+1 Thanks @chmanie! fixed my problem too

+1 Thanks, rejecting fixed it for me!

+1 .thanks that fixed my problem .

+1 Thanks, rejecting fixed it for me!

@chmanie's solution works for me.

@chmanie solution fixed the issue. There should be a note regarding this in Angularjs docs.

@chmanie 谢谢啦~

Was this page helpful?
0 / 5 - 0 ratings

Related issues

guyandtheworld picture guyandtheworld  ·  3Comments

ashclarke picture ashclarke  ·  3Comments

jtorbicki picture jtorbicki  ·  3Comments

kishanmundha picture kishanmundha  ·  3Comments

WesleyKapow picture WesleyKapow  ·  3Comments