Prototype JS swallows errors in dom:loaded, and ajax callbacks?

Posted by WishCow on Stack Overflow See other posts from Stack Overflow or by WishCow
Published on 2010-03-23T21:47:47Z Indexed on 2010/03/23 21:53 UTC
Read the original article Hit count: 393

I can't figure out why prototype suppressess the error messages in the dom:loaded event, and in AJAX handlers.

Given the following piece of HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Conforming XHTML 1.1 Template</title>
        <script type="text/javascript" src="prototype.js"></script>
        <script type="text/javascript">
            document.observe('dom:loaded', function() {
                console.log('domready');
                console.log(idontexist);
            });
        </script>
    </head>
    <body>
    </body>
</html>

The domready event fires, I see the log in the console, but there is no indication of any errors whatsoever. If you move the console.log(idontexist); line out of the handler, you get the

idontexist is not defined

error in the console. I find it a little weird, that in other event handlers, like 'click', you get the error message, it seems that it's only the dom:loaded that has this problem.

The same goes for AJAX handlers:

new Ajax.Request('/', {
    method: 'get',
    onComplete: function(r) {
        console.log('xhr complete');
        alert(youwontseeme);
    }
});

You won't see any errors. This is with prototype.js 1.6.1, and I can't find any indication of this behavior in the docs, nor a way to enable error reporting in these handlers.

I have tried stepping through the code with FireBug's debugger, and it seems to jump to a function on line 53 named K, when it encounters the missing variable in the dom:loaded handler:

K: function(x) { return x } 

But how? Why? When? I can't see any try/catch block there, how does the program flow end up there?

I know that I can make the errors visible by packing my dom:ready handler(s) in try/catch blocks, but that's not a very comfortable option. Same goes for registering a global onException handler for the AJAX calls.

Why does it even suppress the errors? Did someone encounter this before?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about prototypejs