views:

852

answers:

3

I am currently working on a web application, I have a JS logging mechanism that Handles Javascript error that are not caught by the js code inside the page. I am using window.onerror to catch all such errors and log them else where.

However, Problem is with Opera which does not have window.onerror event. one approach I could think of is, to string process all js functions code and insert try catch blocks inside those functions after body load. It does not work in many cases though, But, It at least works to some extent.

I am sure this approach sucks, But, I could not think of anything better. Please advise.

Update: For now, I am calling the code below to Catch as many errors as I could.

function OnBodyLoad()
{
        var allElements = document.getElementsByTagName("*");
        for(var cnt = 0;cnt < allElements.length;cnt++)
        {
            RegisterAllEvents(allElements[cnt]);
        }
}
function RegisterAllEvents(objToProcess){
    for(var cnt = 0;cnt < objToProcess.attributes.length;cnt++){
        if(IsAttributeAnEvent(objToProcess.attributes[cnt].name))
        {
            objToProcess.attributes[cnt].value =  'try{'+objToProcess.attributes[cnt].value+'}catch(err){LogError("'+ objToProcess.id+'"'+ ',err);}';
        }
    }
}
A: 

This also happens on Safari, AFAIK.

What you could certainly do is create a global try/catch block for all JS code instead of text parsing - which could be tricky if you come into things like:

(function ($){
  $(function (){
  });
})(jQuery);
Seb
are you saying something like this will work? It did not :try{function CauseError(){//do somethin to throw errordocument.getElementById('hjsdfkas').yeye = 'yyryr';alert('test');}}catch(e){alert('an error occured');}</script>
Kalyan Ganjam
Yeah, that should work :)
Seb
+1 for happens in Safari too.
scunliffe
+2  A: 

Opera's Dragonfly supports remote debugging. You might be able to hack it (it's all written in JavaScript) and log errors yourself (unfortunately the protocol isn't published yet).

However it might be much easier to bug Opera Software to implement onerror support.

porneL
+1 for file a bug with Opera. `window.onerror` was defined in JavaScript 1.1 (well over a decade ago)
scunliffe
A: 

I am using jQuery to do handle the error and it seem to work so far. Thankyou for the answers

Kalyan Ganjam