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

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -