Get the full call stack trace of $http calls

后端 未结 3 995
不思量自难忘°
不思量自难忘° 2021-02-05 09:03

Let\'s say someone wrote a method like this in a file called app.js trying to peform an XHR request angainst a non existing url:

app.controller(\'Ma         


        
3条回答
  •  悲哀的现实
    2021-02-05 09:58

    One option that came in my mind is to create a module for $http debugging that you can add it as a dependency in your main module whenever you need to debug $http calls. There a decorator for the $http service can be registered that will simply log the arguments of the call a forward it to the $http service. There a breakpoint can be set too.

    I have created a simple working example here. I hope it will help.

    Example $http logger decorator implementation:

    var httpDebugging = angular.module('http-debugging', []);
    
    httpDebugging.decorator('$http', function ($delegate) {
      var debugAware = function (fnCallback) {
        return function () {
            var result = fnCallback.apply(this, arguments);    
            console.log('$http decorator: ', arguments);
            return result;
        };   
      }; 
    
      for(var prop in $delegate) {
        if (angular.isFunction($delegate[prop])) {
            $delegate[prop] = debugAware($delegate[prop]);
        }
      }
    
      return $delegate;
    });
    

提交回复
热议问题