开发者

Ajax Error Reporting With PrototypeJS

I am won开发者_StackOverflow社区dering if there is script or something out there that will report javascript/PrototypeJS errors that happen in Ajax calls. It can be time consuming to fill code with console.log and/or alerts. It would be great if there was something like a browser plugin that would do it.

Anybody have any tools or tips?


Firefox has Firebug.
Chrome has Developer Tools (already built in).
Internet Explorer has Developer Toolbar (already built in).


To catch the errors in script you can use Ajax.Responders,

Ajax.Responders.register({
  onException: function(request, exception) {
    if (window.console) console.log(exception);
  }
});


You mean like, http errors? Some http errors are logged to firebug, like 404 or 500.

You can extend Ajax.Request and make it report any http response status you want without having to repeat code.

Ajax.MyRequest = Class.create(Ajax.Request, {
    initialize: function($super, url, options) {
        function debuggerHandler(response) {
            if(console && console.error) {
                console.error("ERROR: " + response.responseText);
            }
        }

        var debuggers = ["onFailure"]; //add any custom http status code

        options = Object.clone(options) || {};
        var handler;
        var old;
        for(var d in debuggers) {
            handler = debuggers[d];
            if(options[d]) {
                old = options[d];
                handler = function(response) {
                    old(response);
                    debuggers[d](response);
                }
            }
            options[d] = handler;
        }

        $super(url, options);
    }
});

Then, instead of calling Ajax.Request, you call Ajax.MyRequest and every ajax call will go through the debugger handlers and through any handler that you want to treat the error individually. Like:

new Ajax.MyRequest("/thisresourcedoesnotexist");

Will throw a 404 and log to console.error.

new Ajax.MyRequest("/thisresourcedoesnotexist", {
    on404: function() {
        console.error("But I want to treat this one");
    }
});

Will throw a 404, execute the custom handler and log to console.error.

There is a number of ways to improve this approach. This is just the general idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜