18

Now my task is to rewrite $exceptionHandler provider so that it will output modal dialog with message and stop default event.

What I do:

in project init I use method .provider:

.provider('$exceptionHandler', function(){

//and here I would like to have rootScope to make event broadcast

})

standart inject method does not work.

UPD: sandbox - http://jsfiddle.net/STEVER/PYpdM/

Stepan Suvorov
  • 25,118
  • 26
  • 108
  • 176

3 Answers3

31

You can inject the injector and lookup the $rootScope.

Demo plunkr: http://plnkr.co/edit/0hpTkXx5WkvKN3Wn5EmY?p=preview

myApp.factory('$exceptionHandler',function($injector){
    return function(exception, cause){
        var rScope = $injector.get('$rootScope');
        if(rScope){
            rScope.$broadcast('exception',exception, cause);
        }
    };
})

Update: add .provider technique too:

app.provider('$exceptionHandler', function() {
  // In the provider function, you cannot inject any
  // service or factory. This can only be done at the
  // "$get" method.

  this.$get = function($injector) {
    return function(exception,cause){
      var rScope = $injector.get('$rootScope');
      rScope.$broadcast('exception',exception, cause);  
    }
  };
});
checketts
  • 14,167
  • 10
  • 53
  • 82
  • While this resolved the issue, if you need the `$rootScope` injected into a provider configuration (such as the .provider example here: https://gist.github.com/Mithrandir0x/3639232) then this method will not work. – Ben May 22 '13 at 14:39
  • It seems to work ok. Here is a plunkr with it: http://plnkr.co/edit/svRUruKSzuLcBPAWa0ro?p=preview – checketts May 22 '13 at 21:05
  • I guess we couldn't at the time but now, you can just do `this.$get = function($rootScope)` afaik – Ellone Jan 06 '16 at 15:52
1

My way of doing this - using a decorator and reverting to the previous exception handler on unknown errors:

app.config(function ($provide) {
  $provide.decorator('$exceptionHandler', function($delegate, $injector) {
    return function (exception, cause) {
      if (ICanHandleThisError) {
        var rootScope= $injector.get('$rootScope');
        // do something (can use rootScope)
      } else
       $delegate(exception, cause);
    };
  });
});
Amit Portnoy
  • 5,957
  • 2
  • 29
  • 30
-2

You need to inject the $rootScope:

.provider('$exceptionHandler', '$rootScope', function(){

//and here I would like to have rootScope to make event broadcast

})

Is this what you tried? And if so do you have an error message or a jsfillde/plnkr to see why it failed?

martijnve
  • 993
  • 2
  • 9
  • 23
  • Yes but if it does not work you are either using it wrong causing it to have no visible effect or it should produce an error. If you make a jsfiddle it will be much easier for us to help you. – martijnve Feb 19 '13 at 11:00
  • To the people who keep downvoting me: The info I asked for was only added after I asked for it. OP then accused me of asking for info he had already added. – martijnve Oct 27 '15 at 13:52