Asqan
Asqan

Reputation: 4489

Interceptors are not triggered

I'm trying to implement a global authorization handling for my app.

Here you see my code:

.factory('Request', ['$resource', 'general',
        function ($resource) {
            return $resource(baseURL + ':resourceName/', {}, {
                get : {
                    method : 'GET',
                    isArray : true,
                    transformResponse : function (data, headers) {
                        return JSON.parse(data).data;
                    },
                    interceptor: {
                        request : function (data) {
                            console.log("test");
                        },
                        requestError : function (data) {
                            console.log("test");
                        },
                        response : function (data) {
                            console.log("test");
                        },
                        responseError : function (data) {
                            console.log("test");
                            if(data.state == 401){
                                general.logIn({},false,function(){});
                            }
                        }

                    }
                }
            });
        }
    ]);

However, none of the interceptors is triggered. What is wrong with code?

Upvotes: 1

Views: 564

Answers (3)

Simon
Simon

Reputation: 1

you have missed to insert the "general" into the service function, so the

 responseError : function (data) {
                        console.log("test");
                        if(data.state == 401){
                            general.logIn({},false,function(){});
                        }
                    }

does not work at all

Upvotes: 0

AranS
AranS

Reputation: 1891

Taken from Angular documentation regarding $resource:

interceptor - {Object=} - The interceptor object has two optional methods - response and responseError. Both response and responseError interceptors get called with http response object. See $http interceptors.

If you'd like to work with all four interceptors I recommend you configure your app with httpProvider. You can push to the interceptors array all 4 possibilities.

Something like this:

m.config(["$httpProvider", ($httpProvider: ng.IHttpProvider) => {
            $httpProvider.interceptors.push(["$q" , (q: ng.IQService) => {

                    return {
                        'request': (config) => {
                            ..Handle request...

                            return config;
                        },

                        'requestError': (rejection) => {
                            ..Handle requestError...

                            return $q.reject(rejection);
                        },

                        'response': (response) => {
                            ..Handle response...

                            return response;
                        },

                        'responseError': (rejection) => {
                            ..Handle responseError...

                            return $q.reject(rejection);
                        }
                    }
                }
            ]);
       }]);

Upvotes: 1

Brian
Brian

Reputation: 5059

According to the Angular documentation, an interceptor when used in $resource only has two valid methods for the interceptor object:

interceptor - {Object=} - The interceptor object has two optional methods - response and responseError. Both response and responseError interceptors get called with http response object. See $http interceptors.

See: https://docs.angularjs.org/api/ngResource/service/$resource

If you require the request methods as well, you would have to follow the documentation here: https://docs.angularjs.org/api/ng/service/$http

Also, I noted that you have defined responseError twice, instead of response and responseError:

responseError : function (data) {
    console.log("test");
},
responseError : function (data) {
    console.log("test");
    if(data.state == 401){
        general.logIn({},false,function(){});
    }
}

Upvotes: 2

Related Questions