1

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?

13
  • You may want to consider firing off an event, and picking it up in a service and doing the execution from there Commented Jun 28, 2016 at 10:51
  • Don't use rootscope, create your own event aggregator... Commented Jun 28, 2016 at 10:52
  • Can you show full errorInjector code? It seems that it is not full implementation. Commented Jun 28, 2016 at 10:58
  • We don't need full errorInjector code, there is enough there Commented Jun 28, 2016 at 10:59
  • 1
    @ottercoder if you do not understand @CallumLinington code it seems that you do not understant DI in angular, thats why you have '$injector', function ($q, $injector) which should be '$q', '$injector', function ($q, $injector) and thats probably your problem here Commented Jun 28, 2016 at 12:50

3 Answers 3

1

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 now main app controller

(function (angular) {
    'use strict';

    angular.module('controllers').controller("globalCtrl", [
        '$rootScope',
        '$http',
        function ($rootScope, $http) {
            $rootScope.$on('theTokenWillDieSoon', function () {
                // http from here
            });
        }]);
})(angular);
Sign up to request clarification or add additional context in comments.

2 Comments

Circular dependency found: $http <- errorLauncher <- httpInterseptor <- $http <- $templateFactory <- $view <- $state :/
try the $emit from error handler then ) hope it will help
0

.factory('errorInjector',['$injector', function ($q, $injector) { .... }]);

Change To:

.factory('errorInjector',['$q', function ($q) { .... }]);

Comments

0

So I've done it with service. Thanks everyone!

interceptor:

.factory('errorInjector',['$injector', function ($q, $injector) {

    var errorInjector = {
        'response': function (response) {
            ....
        },
        'responseError': function (rejection) {
            if (JSON.stringify(rejection.data.errorCode) === JSON.stringify(101)) {
                var refreshTokenService = $q.get('refreshTokenService');
                refreshTokenService.refreshTokenService();
                $.notify({message: data.data.detailMessage}, {type: 'warning'});
            }
            return $q.reject(rejection);
        }
    };

    return errorInjector;
}]);

refreshTokenService:

.service('refreshTokenService', ['$http', function ($http) {

    this.refreshTokenService = function () {
        $http.post('/refresh').then(
            function success(response) {
               .....
            },
            function error(data) {
                .....
            }
        );
    };

}]) ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.