Bluefire
Bluefire

Reputation: 418

Using $promise.then() after Interceptor erroneously calls successCallback

i want to use $promise.then(successCallback,errorCallback) after the interceptor did the basic message handling. Somehow it always runs into the successCallback and never into the errorCallback if i get error 400,401 etc. If i remove the interceptor it works fine. I also found out that i could create a new promise in the interceptors to solve this problem but i think this is not the best way to go. In my case i only want an interceptor for some basic message handling and then continue in the main controller with the main logic. This is currently not possible because i always run into the successCallback if i get an error. Did i understand something wrong? Here an example what i want:

var resourceObj = $resource("url/abc/:action/:id", {}, {
    getMember: {
        method: 'GET',
        params: { action: 'member' },
        interceptor: {
            responseError: function(response) {
                console.log("Interceptor responseError");
                //Show some default error messages here
                return response; //create new promise with $http(response); to solve the problem? 
            },
            response: function(response) {
                console.log("Interceptor responseSuccess");
                //Show some default success messages here
                return response; //create new promise with $http(response); to solve the problem? 
            }
        }
    }
});
myModule.controller('myCtrl', function($scope) {
    $scope.checkMember() = function(memberId) {
        resourceObj.getMember({ id: memberId }, {}).$promise.then(
            function(responseOK) { //successCallback
                console.log(responseOK);
                $scope.testMember = responseOK.data; // or if no interceptor is used responseOK.toJSON()
                //do some stuff here after async call is finished               
            },
            function(responseError) { //errorCallback, never called in error case if an interceptor is used. 
                console.log(responseError);
                //do maybe some advanced error handling here after async call is finished
            }
        );
    }
});

Upvotes: 2

Views: 57

Answers (1)

georgeawg
georgeawg

Reputation: 48968

It is important to re-throw the error response.

var resourceObj = $resource("url/abc/:action/:id", {}, {
    getMember: {
        method: 'GET',
        params: { action: 'member' },
        interceptor: {
            responseError: function(response) {
                console.log("Interceptor responseError");
                //Show some default error messages here
                ̶r̶e̶t̶u̶r̶n̶ ̶r̶e̶s̶p̶o̶n̶s̶e̶;̶
                //IMPORTANT re-throw error response
                throw response;
            },
            response: function(response) {
                console.log("Interceptor responseSuccess");
                //Show some default success messages here
                return response;
            }
        }
    }
});

If the error response is simply returned, it will be erroneously converted from a rejection to a success.

Upvotes: 2

Related Questions