admin管理员组文章数量:1290971
I've setup a window.onerror
handler to catch errors. Its purpose? To concat a string for each time an error occurs, then another handler window.onunload
sends it off to the Server. (Presumably with all the errors).
Problem is, it's only able to send the first error over, because after the onerror
handler event fires for the first time, the error isn't caught and thus the script terminates...
Is there a 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
}
})(),
I've setup a window.onerror
handler to catch errors. Its purpose? To concat a string for each time an error occurs, then another handler window.onunload
sends it off to the Server. (Presumably with all the errors).
Problem is, it's only able to send the first error over, because after the onerror
handler event fires for the first time, the error isn't caught and thus the script terminates...
Is there a 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
}
})(),
Share
Improve this question
edited Feb 28, 2012 at 23:38
williamsandonz
asked Feb 28, 2012 at 23:14
williamsandonzwilliamsandonz
16.4k23 gold badges106 silver badges189 bronze badges
3
- Please attach some code. – Roko C. Buljan Commented Feb 28, 2012 at 23:18
- Say it again sam, that was the first thing i thought about, Yes, use try/catch blocks. – Codebeat Commented Feb 29, 2012 at 0:07
- Try and catch only works for a certain section. I want it for all errors, you can create a stupid pass through function but don't want to do that. – williamsandonz Commented Feb 29, 2012 at 1:13
1 Answer
Reset to default 10Add it in a seperate <script>
tag, so that window.onerror
is not bothered by script errors in other script tags.
DEMO
<script>
var trackErrorMsg = '';
window.onerror = function(msg, url, lineNo) {
trackErrorMsg += msg;
}
</script>
Please Note the above doesn't stop error being logged in the browser console.. The above code is just to track the errors.. You should use try...catch to handle errors in any specific sections.
DEMO - To show that it doesn't work when you have it on the same execution context.
本文标签: javascriptCatching errors inside windowonerrorStack Overflow
版权声明:本文标题:javascript - Catching errors inside window.onerror? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741510849a2382597.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论