AngularJS service not invoking error callback on save() method

12,748

Solution 1

I found the following in the ngResource source code

$http({method: 'GET', url: '/someUrl'}).
    success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
    }).
    error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with status
        // code outside of the <200, 400) range
    });

I am kind of confused about the range notation but it seems it should actually call the error method. Maybe you found a bug.

Solution 2

Had the same problem and nothing here worked. Turned out I had an custom debug-interceptor that didn't explicitly return a $q.reject(response).

Apparently every custom debug-interceptor completely overwrites the default behavior.

See https://github.com/angular/angular.js/issues/2609#issuecomment-44452795 for where I found the answer.

Solution 3

I had troubles with the error callback as well, but it appears that in more recent versions of AngularJS, the error callback method must now be implemented something like this:

SomeResource.query({}, angular.noop, function(response){
  $scope.status = response.status; 
});

Source + more detailed description: https://groups.google.com/d/msg/angular/3Q-Ip95GViI/at8cF5LsMHwJ

Also, in response to the comments on Flek's post, it seems that now only responses between 200 and 300 are not considered an error.

Solution 4

I couldn't get Alter's answer to work, but this worked for me:

user.$save(function (user, headers) {
                    // Success
                    console.log("$save success " + JSON.stringify(user));
                }, function (error) {
                    // failure
                    console.log("$save failed " + JSON.stringify(error))
                });
Share:
12,748

Related videos on Youtube

chubbsondubs
Author by

chubbsondubs

Updated on September 15, 2022

Comments

  • chubbsondubs
    chubbsondubs over 1 year

    So I'm using angularjs restful service $resource and I'm calling $save function. However, the error callback I pass to it is not being called. The server is sending a 418 error which I thought since it's NOT 200 would result in the error callback being invoked. But, it never does. I can't find any documentation stating what http error codes will result in the error callback being called.

    Here is my code:

    var modalScope = $scope.$new();
    modalScope.showPassword = false;
    modalScope.message = null;
    modalScope.user = new User();
    
    modalScope.submit = function(user) {
        user.$save( {}, function(data,headers) {
            // do the success case
        }, function(data,headers) {
            // do the error case                
        });
    };
    

    The modalScope.user is being passed to the submit function defined. So what's the problem why this error callback isn't being called?

    • Ben Lesh
      Ben Lesh over 11 years
      I think I found the problem, your webserver is a teapot.
  • Adhik Mewada
    Adhik Mewada almost 9 years
    Isn't $save() an instance action making the error callback the third parameter?
  • Dennis Hackethal
    Dennis Hackethal over 8 years
    Ran into the same issue. Thanks for this!
  • dimiguel
    dimiguel almost 8 years
    This saved me a lot of time. The problem was in one of our interceptors. Thank you