1
votes

So I have pulled the interceptor straight from the angular HTTP documentation and yet this still doesn't work. The "request" and "response" functions get called ,but never the "requestError" or the "responseError".

myApp.config(['$httpProvider', function ($httpProvider) {
        $httpProvider.interceptors.push(function ($q) {
            return {
                'request': function (config) {
                    return config; //gets called
                },

                'requestError': function (rejection) {
                    return $q.reject(rejection); //Never gets called
                },

                'response': function (response) {
                    return response; //gets called
                },

                'responseError': function (rejection) {
                    return $q.reject(rejection); //Never gets called
                }
            };
        });
    }]);

On the server I am returning a 400, but really any error would do. And here is the service

User.publicProfileGetProfile = function (value, type) {
    return $http({
        url: '/public/profile/' + type + '/' + value,
        method: 'GET'
    }).then(function (response) {
        return response;
    }, function(error){
        return error;
    });
};

No error functions are being called and every response goes through the response function. The standard angular error is displayed with the Bad Request (400) as usual. When the 400 error is returned, it is simply 'undefined' through the 'response' function in the interceptor.

Let me know if I've forgotten to include any important information.

2

2 Answers

0
votes

By using return, the error handler is converting the rejection to a success. Instead use throw to chain the rejection.

User.publicProfileGetProfile = function (value, type) {
    return $http({
        url: '/public/profile/' + type + '/' + value,
        method: 'GET'
    }).then(function onSuccess(response) {
        return response;
    }, function onReject(error){
        //return converts rejection to success
        //return error;

        //use throw to chain rejection
        throw error;
    });
};
0
votes

When I saw that the JSFiddle (from @georgeawg) was working properly, I made sure mine looked exactly the same. When it didn't work, I looked around to see if I had any other interceptors that might cause problems. I had another interceptor that was being hit first and returning any errors as responses, then they would go through this one and it would process it as a successful response. I removed it and everything seems to be working correct now!