admin管理员组

文章数量:1321426

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"
".dtd">
<html xmlns="">
    <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 plete');
        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 fortable 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?

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/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3/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 plete');
        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 fortable 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?

Share Improve this question edited Dec 27, 2011 at 17:58 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Mar 23, 2010 at 21:47 K. NorbertK. Norbert 10.7k5 gold badges51 silver badges48 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

after a while i found that prototype redirects all exceptions to onException handler:

  new Ajax.Request('/ajax_html_echo', {
    method: 'get',
    onComplete: function(r) {
        console.log('xhr plete');
        alert(undefinedVar)
    },
    onException: function(request,e){
        console.log(e.message); // prints undefinedVar is not defined
    }
});

more info here

http://www.prototypejs/api/ajax/options

onException Triggered whenever an XHR error arises. Has a custom signature: the first argument is the requester (i.e. an Ajax.Request instance), the second is the exception object.

You can also re-throw the exception from onException and you'll get it in the same way as if it happened outside the call

onException: function(request,e){throw e;}

Worked for me

Ajax.Responders

A repository of global listeners notified about every step of Prototype-based Ajax requests.

http://api.prototypejs/ajax/Ajax/Responders/

Ajax.Responders.register({
  onCreate: function() {
    Ajax.activeRequestCount++;
  },
  onComplete: function() {
    Ajax.activeRequestCount--;
  }
});

本文标签: javascriptPrototype JS swallows errors in domloadedand ajax callbacksStack Overflow