javascript - Do $http request from Angular Interceptor? -


i'm trying implement angular interceptor exceptions. 1 @ least. have token , when old enogh backend throws tokenalmostexpired exception. exception contains errorcode = 101. in interceptor i'm checking code 101 , need send post request backend's /refresh endpoint 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 problem @ interceptor level, can't give dependency on $http, because there's circular dependency found: $http <- errorinjector <- $http <- $templatefactory <- $view <- $state

$scope

and can't put getrefreshtoken() function $scope, because $scope dependency gives 'circular dependency'.

$injector

var http = $injector.get('$http'); 

doesn't work well, , gives me erorr.

so how can catch exception in interceptor , $http request there?

interceptor

(function (angular) {     'use strict';      angular.module('services').factory("httpinterceptor", [         'errorlauncher',         '$q',         function (errorlauncher, $q) {             return {                 'requesterror': function (rejection) {                     if (rejection.status === 101) {                         errorlauncher.pushinerrormessage(rejection);                     }                     return $q.reject(rejection);                 },                 'responseerror': function (rejection) {                     if (rejection.status === 101) {                         errorlauncher.pushinerrormessage(rejection);                     }                     return $q.reject(rejection);                 }             };         }]); })(angular); 

and error handler service

(function (angular) {     'use strict';      angular.module('services').factory("errorlauncher", [         '$rootscope',         function ($rootscope) {             return {                 'pushinerrormessage': function (rejection) {                     $rootscope.$emit('thetokenwilldiesoon');                 }             };         }]); })(angular); 

and main app controller

(function (angular) {     'use strict';      angular.module('controllers').controller("globalctrl", [         '$rootscope',         '$http',         function ($rootscope, $http) {             $rootscope.$on('thetokenwilldiesoon', function () {                 // http here             });         }]); })(angular); 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -