Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: Catch all response status 500

This may seem like a strange request. I was wondering if there is a way using a $http interceptor to catch the first URL that has a response status of 500, then stop all subsequent requests and processes and do something?

like image 825
Ben Kilah Avatar asked Oct 03 '13 01:10

Ben Kilah


1 Answers

Thomas answer is correct, but this solution is currently deprecated. You should do something like this answer.

app.factory('errorInterceptor', function ($q) {

    var preventFurtherRequests = false;

    return {
        request: function (config) {

            if (preventFurtherRequests) {
                return;
            }

            return config || $q.when(config);
        },
        requestError: function(request){
            return $q.reject(request);
        },
        response: function (response) {
            return response || $q.when(response);
        },
        responseError: function (response) {
            if (response && response.status === 401) {
                // log
            }
            if (response && response.status === 500) {
                preventFurtherRequests = true;
            }

            return $q.reject(response);
        }
    };
});

app.config(function ($httpProvider) {
    $httpProvider.interceptors.push('errorInterceptor');    
});
like image 105
Zanon Avatar answered Oct 06 '22 00:10

Zanon