I'm trying to implement Angular Interceptor for Exceptions. For one at least. I have a token and when it old enogh backend throws TokenAlmostExpired exception. This exception contains errorCode = 101. In the interceptor I'm checking that code is 101 and then I need to send POST request to backend's /refresh endpoint so I could refresh token.
.factory('errorInjector',['$injector', function ($q, $injector) { var vm = this; var errorInjector = { 'response': function (response) { console.log(response); return response; }, 'responseError': function (rejection) { if (JSON.stringify(rejection.data.errorCode) === JSON.stringify(101)) { vm.getRefreshToken(); } return $q.reject(rejection); } }; return errorInjector; }]); and
.config(['$httpProvider', function ($httpProvider) { $httpProvider.interceptors.push('errorInjector'); }]); $http
But there's a problem at interceptor level, that I can't just give it dependency on $http, because there's Circular dependency found: $http <- errorInjector <- $http <- $templateFactory <- $view <- $state
$scope
And I can't put getRefreshToken() function to $scope, because $scope dependency also gives 'Circular dependency'.
$Injector
var http = $injector.get('$http'); doesn't work as well, and gives me erorr.
So how can I catch exception in interceptor and then do a $http request from there?
'$injector', function ($q, $injector)which should be'$q', '$injector', function ($q, $injector)and thats probably your problem here