Couple questions for the jQuery internals-aware folks.  This is a bit
long, sorry for that.

I'm working on beefing up error handling in a web application.  I want
to ensure that I'm catching all exceptions and handling them via my
own UI system.  The window.onerror function is part of the solution,
but there's a known problem with Firefox (https://bugzilla.mozilla.org/
show_bug.cgi?id=312448) that prevents onerror being called when the
exception is thrown inside an event handler.

In looking for a solution that didn't require mucking about with each
instance of event binding logic throughout the app, I ran across what
seems like a reasonable solution here: (http://blogs.cozi.com/tech/
2008/04/javascript-error-tracking-why-windowonerror-is-not-
enough.html).  The general idea is to override bind() and replace the
passed in handler function with a function that wraps the original
handler function in a try-catch block.

So the code would look something like this:

var jqBindFn = jQuery.fn.bind;
$.fn.bind = function(type, data, fn) {
   if (!fn && data && typeof data === 'function') {
      fn = data;
      data = null;
   }
   if (fn) {
      var ofn = fn;
      var wfn = function() {
      try {
        ofn.apply(this, arguments);
      }
      catch ( ex ) {
        someGlobalErrHandler( ex );
       }
     };
     fn = wfn;
   }
   return jqBindFn.call(this, type, data, fn);
};

Since all other event binding goes through bind(), it seems like this
will easily add exception handling to all event handlers in my app.

So I have two questions about this:

(1) I haven't been using Javascript terribly long, so I'm not really
sure if this approach has any significant costs or downsides.  I mean,
there's a performance hit by having the extra function calls.  And
again I suppose for the exception handling.  But are there potential
memory leak issues or is the performance hit likely to increase the
longer a user uses an app with that "fix" in it?

(2) It sort of seems like Mozilla isn't in any giant hurry to fix
there bug.  It probably doesn't make sense to add code to jQuery core
to work around such an obscure bug for it's own sake.  But I wonder if
adding some exception handling capability to jQuery core makes any
sense?  It could do what it does for other areas of Javascript which
is mitigate the browser differences.  To be complete, it would need to
employ onerror, add a try-catch block to document.ready(), and do
something similar to the above for event handlers (only in mozilla).

Thoughts?  Is this just stupid?

Reply via email to