I’m using window.onerror, and window.onbeforeunload to pass all the errors encountered in the users session with AJAX, I’m trying to test it, and I need to create some javascript errors to test if it is catching it I’ve tried things like var a = b; (where b doesn’t exist) problem is these errors seems to be fatal and stop any further processing…
Can anyone think of a decent way to cause some errors without stopping the processing of the script?
ErrorManager: (function () {
function Init(message) {
InitErrorHandler();
InitAjaxHandler();
}
function InitErrorHandler() {
Data.ErrorHandlerText = "";
Data.ErrorHandlerCount = 0;
window.onerror = function(errorMessage, url, line) {
Data.ErrorHandlerText +"Error: "+(Data.ErrorHandlerCount+1)+" \n\n";
//Get error specific info
Data.ErrorHandlerText += escape(errorMessage) + "\n";
Data.ErrorHandlerText += escape(url) + "\n";
Data.ErrorHandlerText += escape(line) + "\n";
Data.ErrorHandlerCount++;
}
}
function InitAjaxHandler() {
window.onbeforeunload = function() { //when browser closed
if(Data.ErrorHandlerCount > 0) {
SendErrorsToAjax();
}
}
}
function SendErrorsToAjax() {
PrepareErrorsForAjax();
$.getJSON(Interface.PrefixUrl('/sendjserrors/'+Data.ErrorHandlerText), AjaxCallback);
}
function AjaxCallback(response) {
console.log('response of js error ajax request '+response);
}
function PrepareErrorsForAjax() {
var preText = "A user has encountered a few errors: \n\n";
//Get session info
var userAgent, activePageID;
userAgent = escape(navigator.userAgent);
preText += "User agent: "+userAgent+" \n";
if($.mobile.activePage != null) {
activePageID = $.mobile.activePage.attr('id');
preText += "Page ID: "+activePageID+" \n";
}
preText += "\n The following errors were encountered:\n\n";
Data.ErrorHandlerText = preText + Data.ErrorHandlerText;
}
return {
Init: Init,
SendErrorsToAjax: SendErrorsToAjax
}
})(),
How about
You do have to catch it though if you don’t want your program to break.