[英]Module Exceptions in Angularjs and $exceptionHandler
在Angular 1的$exceptionHandler
文档中,它说:
角度表达式中任何未捕获的异常都委托给此服务。
https://code.angularjs.org/1.3.20/docs/api/ng/service/ $ exceptionHandler
但是,当我收到模块初始化错误时( Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to: ...
它似乎没有通过$ exceptionHandler记录下来。
我希望能够通过Raven将这些错误记录到Sentry中,并且在找到解决方案时遇到了麻烦。
这是我要实现的示例:
// Main App module angular.module('app', ['app.welcome', 'app.contact']) .factory('$exceptionHandler', [function() { return function myExceptionHandler(exception, cause) { // why does the module initialisation error not get caught here..? console.log("[ERROR] ", exception.message); }; }]); // a module I forgot to load.. // app.module('app.contact', []); // another sub module angular. module('app.welcome', []) .controller('ExampleController', ['$scope', '$window', function($scope, $window) { $scope.test = function() { $window.alert('test'); }; }]);
<!doctype html> <html lang="en" ng-app="app"> <head> <meta charset="utf-8"> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.4/angular.min.js"></script> <script src="script.js"></script> </head> <body ng-controller="ExampleController"> <button ng-click="test()">Test</button> </body> </html>
这是一个引发错误的代码段,因为没有注入$q
,请在自定义异常处理程序中执行所需的任何操作。
angular.module('app', []); angular. module('app') .controller('ExampleController', ['$scope', function($scope) { $scope.test = function() { $q.when("foo"); }; }]) .factory('$exceptionHandler', [function() { return function myExceptionHandler(exception, cause) { console.log("[ERROR] ", exception.message); }; }]);
<!doctype html> <html lang="en" ng-app="app"> <head> <meta charset="utf-8"> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.4/angular.min.js"></script> <script src="script.js"></script> </head> <body ng-controller="ExampleController"> <button ng-click="test()">Raise Error</button> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.