Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$location from $exceptionHandler - dependency conflict

I'm trying to implement a very standard task: when an exception occurs, redirect to my /error page.

In a simplified form the code looks like this:

app.factory('$exceptionHandler', ['$location', function($location) {
    return function(exception, cause) {
        $location.path("/error");
    };
}]);

However, AngularJS complains: Circular dependency found: $location <- $exceptionHandler <- $rootScope

This looks like a fundamental limitation, not to allow use of $location when handling exceptions.

But how else can we do it then?

like image 293
vitaly-t Avatar asked Oct 24 '13 00:10

vitaly-t


1 Answers

To get around this you need to call the $injector manually to resolve the dependency at runtime:

app.factory('$exceptionHandler', ['$injector', function($injector) {

    var $location;

    return function(exception, cause) {
        $location = $location || $injector.get('$location');
        $location.path("/error");
    };
}]);
like image 133
tasseKATT Avatar answered Oct 27 '22 09:10

tasseKATT