Google Groups Home
Help | Sign in
custom error handler around browser event loop?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
joanna  
View profile
 More options Feb 8, 8:59 pm
From: joanna <jpowe...@gmail.com>
Date: Fri, 8 Feb 2008 17:59:09 -0800 (PST)
Local: Fri, Feb 8 2008 8:59 pm
Subject: custom error handler around browser event loop?
The development group seemed like the right place for this question;
my apologies in advance if I should have used the broader group.

My company is using jQuery to build a very cool web app. We love it.
We're trying to do as much right as possible, including exception
handling. I've got a custom error handler that I want all errors to go
through so that we can do things like report them back to the server,
add debug alerts, etc.

Everything works great in IE(6/7), but I'm missing a set of errors in
Firefox. These are errors that are thrown during the browser event
loop. I know that's not a jQuery thing, but I wonder if jQuery might
have someplace that I can hook into to put a try, catch around the
whole darn thing. I have not found anything helpful by googling,
though it's hard to know the right query.

Here's an html page that demonstrates the problem. Just update the
location of the jquery.js and it should work fine.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Exception Example</title>

    <script type="text/javascript" src="./jquery-1.2.3.pack.js"></
script>

    <style>
        body { font-family: Tahoma, Arial, sans-serif }
        div { margin: 20px }
        div #errorConsoleMsg {
            font-size: smaller;
            font-weight: bold;
            margin: 20px 60px 20px 60px; }
    </style>

</head>
<body>
    <div>
    Examples of throwing errors from all the places I care about.<br /


    Loading the page should generate two errors:
    <ol>
        <li>&quot;outside document ready handler&quot;</li>
        <li>&quot;inside document ready handler&quot;</li>
    </ol>

    Clicking on the button should generate another: &quot;bad button
error&quot;<br />
    <br />
    The problem is that in Firefox, the error generated by clicking
the
    button is lost. I want it to go through my handleError
    function. It works fine in IE(6,7) because the window's
    error event is triggered. But it never gets to my handler in
Firefox.<br />
    </div>

    <div id="badButtonDiv">
    <button id="badButton">click me to generate an error</button>
    </div>

    <div>
    If I open the
    browser's error console, I can see that the error is caught by
someone:

    <div id="errorConsoleMsg">
        Error: [Exception... &quot;'Error: bad button error' when
calling method:

     [nsIDOMEventListener::handleEvent]&quot;
        nsresult: &quot;0x8057001c
(NS_ERROR_XPC_JS_THREW_JS_OBJECT)&quot;
        location: &quot;&lt;unknown&gt;&quot;
        data: no]
    </div>

    I just want it to be me.
    </div>

    <script type="text/javascript">

    // The single point through which I want all errors to go
    // err: a string or an Error object
    function handleError( err )
    {
        var msg = 'Unknown error';
        if ( err.message )
        {
            msg = err.message;
        }
        else if ( typeof err == 'string' || typeof err == 'String' )
        {
            msg = err;
        }
        alert( 'handleError: ' + msg );
    };

    // Attach handleError to window's onerror event;
    // this doesn't work w/ $(window).error( ... ), but I don't know
why
    window.onerror = function( msg, url, lineNo )
    {
        handleError( msg );
        return true;
    };

    $(document).ready( function() {
        try
        {
            $('#badButton').click( function() { throw new Error("bad
button error") } );
            throw new Error("inside document ready handler");
        }
        catch ( ex )
        {
            handleError( ex );
        }
    } );

    throw new Error("outside document ready handler");

    </script>

</body>
</html>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
dhtmlkitchen@gmail.com  
View profile
 More options Mar 9, 2:45 pm
From: "dhtmlkitc...@gmail.com" <dhtmlkitc...@gmail.com>
Date: Sun, 9 Mar 2008 11:45:43 -0700 (PDT)
Local: Sun, Mar 9 2008 2:45 pm
Subject: Re: custom error handler around browser event loop?

On Feb 8, 6:59 pm, joanna <jpowe...@gmail.com> wrote:

> The development group seemed like the right place for this question;
> my apologies in advance if I should have used the broader group.

Your suspicion that it might be a larger problem is right.

jQuery uses addEventListener, when it is available.

addEventListener callbacks fire independently of one another.
document,addEventListener( "click", cb1, false );
document,addEventListener( "click", cb2, false );

function cb1(){ throw Error("bad."); }
functin cb2(){ console.log("foo"): }

addEventListener usurps onerror.

document,addEventListener( "click", cb1, false );
document,addEventListener( "click", cb2, false );

function cb1(){ throw Error("bad."); }
function cb2(){ console.log("foo"); }

https://bugzilla.mozilla.org/show_bug.cgi?id=312448

Either use a Registry that does not use addEventListener or don't use
onerror and wrap the callback call in try-catch (or both).

Garrett


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
joanna  
View profile
 More options Mar 19, 2:29 pm
From: joanna <jpowe...@gmail.com>
Date: Wed, 19 Mar 2008 11:29:05 -0700 (PDT)
Local: Wed, Mar 19 2008 2:29 pm
Subject: Re: custom error handler around browser event loop?
Thanks for the response; it headed me in a good direction. I wasn't
sure how to use a Registry that avoided addEventListener, and I was
loathe to give up using window.onerror, but I figured I could overload
jQuery.fn.bind. It works! The "inside document ready handler and "bad
button error" errors end up getting tracked twice, but the second
report looks different from the first due to the addition of the url
and lineNo by the window.onerror call to trackError. And no error
escape notice, even in Firefox!

Thanks very much for your help.

-Joanna

Updated html/js code follows. Again, update the location of the
jquery.js and it should work fine.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Exception Example</title>

    <script type="text/javascript" src="../script/third-party/
jquery.js"></script>

    <style>
        body { font-family: Tahoma, Arial, sans-serif }
        div { margin: 20px }
        div #errorConsoleMsg {
            font-size: smaller;
            font-weight: bold;
            margin: 20px 60px 20px 60px; }
    </style>

</head>
<body>
    <div>
    Examples of throwing errors from all the places I care about.<br /


    Loading the page should generate two errors:
    <ol>
        <li>&quot;outside document ready handler&quot;</li>
        <li>&quot;inside document ready handler&quot;</li>
    </ol>

    Clicking on the button should generate another: &quot;bad button
error&quot;<br />
    <br />
    The problem is that in Firefox, the error generated by clicking
the
    button is lost. I want it to go through my trackError
    function. It works fine in IE(6,7) because the window's
    error event is triggered. But it never gets to my handler in
Firefox.<br />
    </div>

    <div id="badButtonDiv">
    <button id="badButton">click me to generate an error</button>
    </div>

    <div>
    If I open the
    browser's error console, I can see that the error is caught by
someone:

    <div id="errorConsoleMsg">
        Error: [Exception... &quot;'Error: bad button error' when
calling method:
     [nsIDOMEventListener::handleEvent]&quot;
        nsresult: &quot;0x8057001c
(NS_ERROR_XPC_JS_THREW_JS_OBJECT)&quot;
        location: &quot;&lt;unknown&gt;&quot;
        data: no]
    </div>

    I just want it to be me.
    </div>

    <script type="text/javascript">

    // The single point through which I want all errors to go
    // err: a string or an Error object
    function trackError( err )
    {
        var msg = 'Unknown error';
        if ( err.message )
        {
            msg = err.message;
        }
        else if ( typeof err == 'string' || typeof err == 'String' )
        {
            msg = err;
        }
        alert( 'tracking error: ' + msg );
    };

    // override jQuery.fn.bind to wrap every provided function in try/
catch
    var jQueryBind = jQuery.fn.bind;
    jQuery.fn.bind = function( type, data, fn ) {
        if ( !fn && data && typeof data == 'function' )
        {
            fn = data;
            data = null;
        }
        if ( fn )
        {
            var origFn = fn;
            var wrappedFn = function() {
                try
                {
                    origFn.apply( this, arguments );
                }
                catch ( ex )
                {
                    trackError( ex );
                    // let error propogate; we didn't handle it, we
just tracked it
                    throw ex;
               }
            };
            fn = wrappedFn;
        }
        return jQueryBind.call( this, type, data, fn );
    };

    // Attach trackError to window's onerror event;
    // use onerror instead of $(window).error( ... ) in order to get
url and lineNo
    window.onerror = function( msg, url, lineNo )
    {
        trackError( [ msg, url, lineNo ].join('; ') );
        // let error propogate; we didn't handle it, we just tracked
it
        return false;
    };

    $(document).ready( function() {
        try
        {
            $('#badButton').click( function() { throw new Error("bad
button error") } );
            throw new Error("inside document ready handler");
        }
        catch ( ex )
        {
            trackError( ex );
            // let error propogate; we didn't handle it, we just
tracked it
            throw ex;
        }
    } );

    throw new Error("outside document ready handler");

    </script>

</body>
</html>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google