$ log decorator with service dependency causes a circular dependency error

In my application, I am writing a decorator for $logso that I can configure functions $log, including calling a third-party service. A third party service introduces $qfor its internal actions. Now this causes a cyclic dependency error:

Uncaught Error: Circular dependency: $q <- tploggerService <- $log <- $exceptionHandler <- $rootScope.

Because it qProvideruses exceptionHandlerProvider, which ultimately uses logProvider, which, it seems to me, causes this. Has anyone encountered similar problems with decorating and is there a solution to work on this or another template to solve this problem?

Here is a simple code demo, rate your help:

///Some third party APP
angular.module('SomeThirdPartyApp', []);

    tploggerService.$inject = ['$q']; //<-- $q is a dependency

    function tploggerService ($q) {

        this.info = function (data) {
            var deferred = $q.defer(); //Doing something...
            //....
            //....
     };
}

angular.module('SomeThirdPartyApp').service('tploggerService', tploggerService);

///--------------------------------------------------------------------------------
///MY APP
angular.module('decorApp', ['SomeThirdPartyApp']);

 angular.module('decorApp').config([
   '$provide', function ($provide) {
     $provide.decorator('$log', ['tploggerService','$delegate', 
       function (tploggerService, $delegate) { //<--- Injecting tpLoggerService causes circular dependency error.
         var _info = $delegate.info;
         //It is no different even if we use $injector
         $delegate.info = function(){
             var args; //doing something with the arguments basically formatting massaging it.
             var customlogmessage; //doing something with args

             tploggerService.info(customlogmessage);
             _info.apply(null, args);
         }
         return $delegate;
       }]);
   }]);
+4
2

$q :

function tploggerService ($injector) {
    var $q;
    this.info = function (data) {
        $q = $injector.get('$q');
        var deferred = $q.defer(); //Yes using defered object. some this performs some actions and some internal stuffs.

        //Doing something...
    };
}

Plunk

+1

, $ $provider.decorator(), , - , . :

$provider.decorator('$log',function($delegate){
  $delegate.info = function(){
     var args = Array.prototype.slice(arguments);
     var deferred = $q.defer(); //Doing something...
     $delegate.info.apply(null,args);
  }
});
0

Source: https://habr.com/ru/post/1538922/


All Articles