Catch bugs inside window.onerror?

I set the window.onerror handler to detect errors. His purpose? To execute a sequence for each time an error occurs, another window.onunload handler sends it to the server. (Presumably with all errors).

The problem is that it is only capable of transferring the first error, because after the first event of the onerror handler onerror an error occurs and the script error ends ...

Is there any way around this?

 ErrorManager: (function () { function Init(message) { InitErrorHandler(); InitAjaxHandler(); setTimeout("Interface.ErrorManager.SendErrorsToAjax()", 8000); } function InitErrorHandler() { Data.ErrorHandlerText = ""; Data.ErrorHandlerCount = 0; window.onerror = function(errorMessage, url, line) { if(IsInIgnoreList(errorMessage)) { return; } Data.ErrorHandlerText += ("Error: "+(Data.ErrorHandlerCount+1)+" <br /><br />"); //Get error specific info Data.ErrorHandlerText += escape(errorMessage) + "<br />"; Data.ErrorHandlerText += escape(url) + "<br />"; Data.ErrorHandlerText += escape(line) + "<br />"; Data.ErrorHandlerCount++; console.log("BOOM"+errorMessage); } } function InitAjaxHandler() { window.onbeforeunload = function() { //when browser closed Data.ErrorHandlerCount > 0 && SendErrorsToAjax(); } } function SendErrorsToAjax() { PrepareErrorsForAjax(); $.getJSON(Interface.Utility.PrefixURL('/ajax/sendfeedback/handlejserrors/'+Data.ErrorHandlerText)); } function IsInIgnoreList(str) { return str.indexOf('nsIDOMSVGLocatable') != -1 ? true : false; } function PrepareErrorsForAjax() { var preText = "<br /><br />A user has encountered a few errors: <br /><br />"; //Get session info var userAgent, activePageID, accountNO, consumerNO; userAgent = escape(navigator.userAgent); preText += "User agent: "+userAgent+" <br />"; if($.mobile.activePage) { activePageID = $.mobile.activePage.attr('id'); preText += "Page ID: "+activePageID+" <br />"; } //Get info that may or may not be set console.log("AUTH JSON "+Data.authJSON); console.log("CONSUMER JSON "+Data.authJSON.consumers); if(Data.authJSON && localStorage.lastConsumerNo) { preText += "Account Number: " +Data.authJSON.accountId+" <br />"; preText += "Consumer Number: "+ localStorage.lastConsumerNo+" <br />"; } preText += "<br /> The following errors were encountered:<br /><br />"; Data.ErrorHandlerText = preText + Data.ErrorHandlerText; } return { Init: Init, SendErrorsToAjax: SendErrorsToAjax } })(), 
+6
source share
1 answer

Add it to a separate <script> tag so that window.onerror does not worry about script errors in other script tags.

Demo

 <script> var trackErrorMsg = ''; window.onerror = function(msg, url, lineNo) { trackErrorMsg += msg; } </script> 

Please note that the above does not stop the error logged in the browser console. The above code is for error tracking only. You should use try ... catch to handle errors in any specific sections.

DEMO - show that it does not work when you have it in the same execution context.

+10
source

Source: https://habr.com/ru/post/909592/


All Articles