javascript - JS handle catching all the errors in all pages with angular -
i using angular js , trying catch errors in pages, problem not catch errors in pages, example doing referenceerror in 1 of controllers , not catch this.
$rootscope.$on("$statechangestart", function() { window.onerror = function( msg, url, line, col, error ) { var = !col ? '' : '\ncolumn: ' + col; += !error ? '' : '\nerror: ' + error; var data = { msg : msg, url : url, line: line + } alert('error'); var suppresserroralert = true; return suppresserroralert; }; }
how can make work?
don't use window.onerror, instead use $exceptionhandler. angular has global $exceptionhandler factory catches errors happen inside controllers, services, etc.
any uncaught exception in angular expressions delegated service.
angular.module('exceptionoverride', []).factory('$exceptionhandler', function() { return function(exception, cause) { exception.message += ' (caused "' + cause + '")'; throw exception; }; });
this example override normal action of $exceptionhandler, make angular exceptions fail hard when happen, instead of logging console.
more info: https://docs.angularjs.org/api/ng/service/$exceptionhandler
nice blog post: http://blog.loadimpact.com/blog/exception-handling-in-an-angularjs-web-application-tutorial/
Comments
Post a Comment