When it comes to the documentation of Angular 1 for $exceptionHandler
, it states:
Any uncaught exception in angular expressions is passed to this service.
https://code.angularjs.org/1.3.20/docs/api/ng/service/$exceptionHandler
However, I have noticed that when encountering a module initialization error (
Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to: ...
), it doesn't seem to be recorded through the $exceptionHandler.
I am looking to log these errors to Sentry via Raven but struggling to find a solution.
Below is an example of what I am attempting to achieve:
// Main App module
angular.module('app', ['app.welcome', 'app.contact'])
.factory('$exceptionHandler', [function() {
return function myExceptionHandler(exception, cause) {
// Why isn't the module initialization error caught here..?
console.log("[ERROR] ", exception.message);
};
}]);
// A module that was not loaded..
// 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>