Reputation: 1499
I am writing a testing suite for javascript and need a way to capture any JS errors and continue processing the rest of the page.
I can't use window.onerror since return true stops the browser from proceeding. I tried using a try {} catch block but the function is run in a window setTimeout() for various reasons and that seems to mess up the try catch block.
For example, I do something like this:
function test(msg, fn) {
$('#output').text(msg);
try {
fn.apply(this);
} catch(e) {
document.write('error'+e);
};
}
and then call it like this
test('trying this', function() { setTimeout('afunction()',100); });
but if afunction() fails the error is not being caught.
Does anyone have any idea on a solution or global error handler that allows me to resume?
Upvotes: 0
Views: 1489
Reputation: 154848
There is window.onerror
, which you can attach a handler function to. If that function returns true
, the error is suppressed: http://jsfiddle.net/pimvdb/qAv9J/2/.
window.onerror = function(e) {
console.log(e);
return true;
};
Upvotes: 2