[英]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.