当试图覆盖$exceptionHandler时,Angular Controller init上会发生7次简单的java

Simple javascript exception happens 7 times on Angular Controller init when trying to override $exceptionHandler

本文关键字:7次 java 简单 init Controller 覆盖 exceptionHandler Angular      更新时间:2023-09-26

我编写了一个工厂来覆盖内置的$exceptionHandler,因为我计划创建一个可以从前端登录的后端服务。但是现在,我只想捕获异常,将其记录到控制台,然后抛出它,因为知道console.log将被服务调用替换。

angular.module('exceptionOverride', []).factory('$exceptionHandler', function() {
    return function(exception) {
        console.log(exception);
        throw exception;
    };
});

在控制器的init函数中,我把

var x = n + 1,所以它会抛出一个错误,由于n是未定义的。

但是,在控制器初始化时,会发生以下情况:

exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:4 ReferenceError: n is not defined(…)
exceptionOverride.js:6 Uncaught ReferenceError: n is not definedinit @ taxAccountNameController.js:17taxAccountNameController @ taxAccountNameController.js:12invoke @ angular.js:4478instantiate @ angular.js:4486(anonymous function) @ angular.js:9151$interpolate.compile @ angular-ui-router.js:4018invokeLinkFn @ angular.js:8789nodeLinkFn @ angular.js:8289compositeLinkFn @ angular.js:7680publicLinkFn @ angular.js:7555updateView @ angular-ui-router.js:3959directive.compile @ angular-ui-router.js:3927invokeLinkFn @ angular.js:8789nodeLinkFn @ angular.js:8289compositeLinkFn @ angular.js:7680publicLinkFn @ angular.js:7555$interpolate.compile @ angular-ui-router.js:4026invokeLinkFn @ angular.js:8789nodeLinkFn @ angular.js:8289compositeLinkFn @ angular.js:7680publicLinkFn @ angular.js:7555updateView @ angular-ui-router.js:3959(anonymous function) @ angular-ui-router.js:3921parent.$get.Scope.$broadcast @ angular.js:16311$state.transitionTo.$state.transition.resolved.then.$state.transition @ angular-ui-router.js:3311processQueue @ angular.js:14745(anonymous function) @ angular.js:14761parent.$get.Scope.$eval @ angular.js:15989parent.$get.Scope.$digest @ angular.js:15800parent.$get.Scope.$apply @ angular.js:16097done @ angular.js:10546completeRequest @ angular.js:10744requestLoaded @ angular.js:10685
angular.js:68 Uncaught Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.4.7/$rootScope/inprog?p0=%24digestREGEX_STRING_REGEXP @ angular.js:68beginPhase @ angular.js:16346parent.$get.Scope.$digest @ angular.js:15780(anonymous function) @ angular.js:16028completeOutstandingRequest @ angular.js:5507(anonymous function) @ angular.js:5784

我的exceptionOverride中的console.log触发7次,然后抛出异常(如预期的那样),但随后我也得到$digest already in progress错误。

我做错了什么?

由于你覆盖了Angular的本地$exceptionHandler,它无法完成它的工作。$exceptionHandler包含比console.logthrow更多的行。它有一些代码可以暂停其他Angular服务或提供商来避免这种情况。