Mimic Window. onerror in Opera using javascript
Asked Answered
S

4

8

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);}';
        }
    }
}
Spinode answered 14/3, 2009 at 12:24 Comment(1)
Thanks for the code! We might end up using that in development mode at our company (if it isn't too slow).Idiotic
B
2

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);
Behm answered 14/3, 2009 at 17:22 Comment(1)
are you saying something like this will work? It did not : try{ function CauseError(){ //do somethin to throw error document.getElementById('hjsdfkas').yeye = 'yyryr'; alert('test'); } }catch(e){ alert('an error occured'); } </script>Spinode
B
9

Opera 11.60+ supports window.onerror.

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).

Bopp answered 15/3, 2009 at 20:37 Comment(3)
+1 for file a bug with Opera. window.onerror was defined in JavaScript 1.1 (well over a decade ago)Monkey
It's a known limitation, no bug report required.Taite
Opera 11.60 supports it, too.Hooten
D
5

you can replace Error.prototype.toString in Opera!

window.onerror = function (msg) {
  // send msg to http://errors.net/log.php, for example
  (new Image()).src = 'http://errors.net/log.php?msg=' + encodeURIComponent(msg);
};
if (({}).toString.call(window.opera) === '[object Opera]') {
 (function () {
   var x = Error.prototype.toString;
   Error.prototype.toString = function () {
     var msg = '';
     try {
       msg = x.apply(this, arguments);
       if (typeof (window.onerror) === "function") {
         window.onerror(msg, typeof (this) === 'object' ? this.stack : '', '');
       }
     } catch (e) {}
     return msg;
   };
 }());
}

seems, it doesn't work for Opera 11.50... only for early versions ...

Dermatologist answered 14/6, 2011 at 10:48 Comment(0)
B
2

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);
Behm answered 14/3, 2009 at 17:22 Comment(1)
are you saying something like this will work? It did not : try{ function CauseError(){ //do somethin to throw error document.getElementById('hjsdfkas').yeye = 'yyryr'; alert('test'); } }catch(e){ alert('an error occured'); } </script>Spinode
D
0

there is a mention here that Opera now supports window.onerror:

http://my.opera.com/ODIN/blog/2011/11/07/what-s-new-in-opera-development-snapshots-4-november-2011-edition

but window.onerror does not seem to work in Opera Mini (e.g. user agent "Opera/9.80 (J2ME/MIDP; Opera Mini/7.1.32422/30.3214; U; en) Presto/2.8.119 Version/11.10"). This makes it really hard to debug javascript on mobiles with Opera Mini.

Dy answered 22/6, 2013 at 20:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.