Error status codes do not get

I am trying to catch an angular resource http error code (! = 200). My service where I defined the resources: (ApiService.js)

.factory('ApiService', function($resource, $http, localStorageService, CONFIG) {

    var base_api_url = api_url = CONFIG.api_url, api_version_prefix = CONFIG.api_version_prefix;

    return {
        userDevices: $resource(api_url+'/requestRegistration/userDevices/:action', {}, {
            registerDevice: {
                method: 'POST',
                params: {
                   action: ''
                }
            },
            verify: {
                method: 'POST',
                params: {
                   action: 'verify'
                }
            },               
        }
    }
});

My controller code:

.controller('LoginCtrl', function(CONFIG, $scope, $state, $ionicPlatform, $ionicPopup, ApiService) {


    $scope.data = {
        username: null
    };

    $scope.registerDevice = function() {
        if($scope.data.username) { 
            var authenticationResponse = ApiService.userDevices.registerDevice({
                username: $scope.data.username
            });

            authenticationResponse.$promise.then(function(result) {
                // this is always fired, even then response code is 400 or 503 :( I am not able to check response status code.
                console.log(result);
                console.log('success!');
            }, function(error){
                // this code is not being exectued even when response status code is different then 200
                // its never executed at all :(
                console.log('error!');
            });
        }
    };


});

When I send a request and receive a 400/503 response code, I believe that the function (error) code should be executed, but it is not.

Instead, my code is executed in $promise.then(function(result)(...), and I cannot detect the response code of the HTTP response.

So my questions are:

  • Why is my error handling function not executing?
  • How to determine HTTP response status codes?
+4
source share
3 answers

.catch . , .catch throw .

   authenticationResponse.$promise.catch(function(error){
        alert('catched error!!!');
        //throw to chain error
        throw error;
    }).then(function(result) {
        // this is always fired, even then response code is 400 or 503 :(
        console.log(result);
        console.log('success!');
        //return to chain data
        return result
    }, function(error){
        // This should be executed when status code is different then 200?
        // its never executed at all :(
        console.log('error!');
        //throw to chain rejection
        throw error;
    });

return throw, undefined. $q , undefined.


ngResource

$resource, :

userDevices: $resource(api_url+'/requestRegistration/userDevices/:action', {}, {
        registerDevice: {
            method: 'POST',
            params: {
               action: ''
            },
            interceptor: {
                response: function (response) {
                    console.log("registerDevice success");
                    console.log(response.status);
                    return response;
                },
                errorResponse: function (errorResponse) {
                    console.log("registerDevice error");
                    console.log(errorResponse.status);
                    throw errorResponse;
                }
            }
        },
        verify: {
            method: 'POST',

, , $http interceptors , throw.

+2

, HTTP- Angular. , HTTP-

+1

.

, ,

, , $http, $resource.

$httpProvider.interceptors.push(function($q) {
     return {
        'responseError': function(response) {
          if (response.status == 400) {
            // Handle 400 error code
          }
          if (response.status == 503) {
            // Handle 503 error code
          }

          // Rejects the derived promise.
          return $q.reject(response);
        }
      };
    });
0

Source: https://habr.com/ru/post/1652603/


All Articles