Logging Errors Remotely - cyclical dependency $http <-> $exceptionHandler

3,029 views
Skip to first unread message

Martin Probst

unread,
Jul 27, 2012, 2:56:38 PM7/27/12
to angular
Hi,

I'm trying to log errors remotely by overriding $exceptionHandler. Essentially I want this:

myModule.factory('$exceptionHandler', ['$http', function($http) {
  return function(exception, cause) {
    $http.post('/errorlog', { something with exception and cause });
  };
}]);

See this fiddle: http://jsfiddle.net/xzDVe/

The message: Circular dependency: $http <- $exceptionHandler <- $rootScope 

The problem is that $http depends on $exceptionHandler. How can I break the cycle? I was playing around with providers (both making an $exceptionHandlerProvider and taking an $httpProvider), but couldn't seem to get them to work.

Martin

Pawel Kozlowski

unread,
Jul 28, 2012, 3:32:22 AM7/28/12
to ang...@googlegroups.com
hi Martin,

On Fri, Jul 27, 2012 at 8:56 PM, Martin Probst <martin...@google.com> wrote:
> Hi,
>
> I'm trying to log errors remotely by overriding $exceptionHandler.
>
> See this fiddle: http://jsfiddle.net/xzDVe/
>
> The message: Circular dependency: $http <- $exceptionHandler <- $rootScope

Yes, since I guess that $http uses $exceptionHandler as its dependency....
You could get a n instance of the $http from the $injector to avoid
this: http://jsfiddle.net/pkozlowski_opensource/xzDVe/1/

This will make DI error go away and things will work more or less OK
but still you've got a kind of catch-22 here... The thing is that you
might have exceptions in the $http calls posting exceptions data....

Cheers,
Pawel

Martin Probst

unread,
Jul 30, 2012, 12:52:30 PM7/30/12
to ang...@googlegroups.com
Thanks, that actually worked. I'm protecting myself against recursive calls to the exception provider with a simple flag value.

I'm still a bit unsure on how providers in AngularJS work; in Guice they are the method of choice to break cycles, but that doesn't seem to work in AngularJS. Am I missing something?

Martin



--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.



Reply all
Reply to author
Forward
Message has been deleted
0 new messages