Error: [$rootScope:inprog]: AngularJS 1.8.2 crashes when trying to $q.reject redirected request in interceptor

52 Views Asked by At

I am using AngularJS v1.8.2 and I am trying to create an interceptor to handle redirects, the idea was taken from this thread

Here is the code of interceptor factory:

.factory('redirectInterceptor', function ($location, $q) {
        return {
            response: function (response) {
                if (typeof response.data === 'string' && response.data.indexOf("<meta name='LoginPage' />") > -1) {
                        $location.path('/Login');
                        return $q.reject(response);
                }
                return response;
            }
        }
    })

But the $q.reject of redirected request, for some reasons breaks my application and an endless amount of the same errors start to happen

Angular.min.js:137 Error: [$rootScope:inprog] http://errors.angularjs.org/1.8.2/$rootScope/inprog?p0=%24digest
    at Angular.min.js:15:168
    at p (Angular.min.js:156:64)
    at m.$digest (Angular.min.js:160:439)
    at m.$apply (Angular.min.js:164:484)
    at Angular.min.js:180:116
    at Yg.completeTask (Angular.min.js:176:231)
    at Angular.min.js:60:229 undefined

I tried to remove $q.reject and it works fine, but in callback of $http call I fail, because I expect getting an object, but I get .html of a login page. Also tried throwing an Error instead of $q.reject but that way I also get an error in console

Angular.min.js:137 TypeError: Cannot read properties of undefined (reading 'ignoreLoadingBar')
    at responseError (Angular.min.js:415:1115)
    at Angular.min.js:149:454
    at m.$digest (Angular.min.js:161:67)
    at m.$apply (Angular.min.js:164:484)
    at k (Angular.min.js:115:445)
    at v (Angular.min.js:121:40)
    at y.onload (Angular.min.js:121:464) 'Possibly unhandled rejection: {}'

I need a way to break this request callback chain and not to break my application

0

There are 0 best solutions below