Ho impostato un gestore window.onerror
per rilevare gli errori. Il suo scopo? Per concatenare una stringa ogni volta che si verifica un errore, un altro handler window.onunload
lo invia al server. (Presumibilmente con tutti gli errori).Errori di cattura all'interno di window.onerror?
problema è, è solo in grado di inviare il primo errore finita, perché dopo gli incendi di evento onerror
gestore per la prima volta, l'errore non viene catturato e quindi lo script termina ...
C'è un modo intorno a questo?
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
}
})(),
Si prega di allegare un po 'di codice. –
Sì, usa i blocchi try/catch. – Christian
Dillo ancora Sam, è stata la prima cosa a cui ho pensato, Sì, usa i blocchi try/catch. – Codebeat