2

I don't know what is happening with angular, it is not catching errors response, I see here questions about this with no answers.

I have a simple http request, I'm getting 400 response and I want to show a message to the user when its happen, the problem is that the part of the 200 is executed.

I have try in two ways:

 this.forgatPassword = function(data) {
      return $http.post('http://someaddress/ForgotPassword', data);  
  }

 $scope.forgatPassword = function() {
        AuthService.forgatPassword($scope.user).then(function(res) {
            $scope.message = true;             
        }, function(error) {
            console.log('rejected');
        });
    }

$scope.forgatPassword = function() {
        AuthService.forgatPassword($scope.user).then(function(res) {
            $scope.message = true;            
        }).catch( function(error) {
            console.log('rejected');
        });
    }
1
  • have you tried using $http({ url:".., method: "POST" }).success(function (data) { console.log("success "+JSON.stringify(data)); }).error(function(data){ console.log("Something went wrong"+JSON.stringify(data)); }); Commented Dec 11, 2014 at 13:31

3 Answers 3

5

I have the same problem and i found out that in my auth interceptor i check the response status. if the response status is 401 i do something, but i forgot to set if not to return the promise, so the interceptor doesnt forward the rejection.

So make sure you have this code in the end:

return $q.reject(rejection);
Sign up to request clarification or add additional context in comments.

Comments

0

Try it like this.

    AuthService.forgatPassword($scope.user)
   .success(function(res) {
        $scope.message = true;            
    })
    .error( function(error) {
        console.log('rejected');
    });

2 Comments

And no errors in the console log? Perhaps it is handled somewhere else. Do you use $httpProvider.interceptors anywhere?
only in the config phase i have general auth interceptor.
0

You can inject $q in your service and return a promise manually.

this.forgatPassword = function(data) {
    var q = $q.defer();
    $http.post('http://someaddress/ForgotPassword', data)
        .then(function(response) {
            q.resolve(response.data);
        },
        function(error) {
            q.reject(error);
        });
    return q.promise;
};

$scope.forgatPassword = function() {
    AuthService.forgatPassword($scope.user).then(function(res) {
        $scope.message = true;             
    }, function(error) {
        console.log('rejected');
    });
}

Comments

Your Answer

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