[AngularJS] Catching errors with $exceptionHandler

The AngularJS $exceptionHandler service allows you to catch and handle unanticipated JavaScript errors in a meaningful way.

So when application is under building process, can create a $exceptionHandler service to log out the uncatch exception.

 

angular.module(‘app‘, [])
    .factory(‘$exceptionHandler‘, function ($injector) {
        return function (exception, cause) {
            var $rootScope = $injector.get(‘$rootScope‘);
            $rootScope.errors = $rootScope.errors || [];
            $rootScope.errors.push(exception.message);
            console.log($rootScope.errors);
        }
    })
    .run(function ($http) {

        function onSuccess (result) {
            console.log(‘hooray data!‘);
            console.log(result.data.length, ‘repos found‘);
            result.count(); // This is no count() method on the result object.
        }

        function onFailure (info) {
            console.log(‘boo error :(‘);
            console.log(info);
        }

        $http.get(‘https://api.github.com/users/bclinkinbeard/repos‘)
            .then(onSuccess, onFailure); // We can catch $http return failure but there could be some uncatch failure in some place
    });

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。