how to log js errors at server

209 views Asked by At

I wanted to know does YUI3 provides any way to try and catch errors functionality, where in after the error is captured we can show some customized error alert and simultaneously log the error at server side with the error exceptions and other details.

Also if this functionality is not there in yui3 then which all frameworks do one need to use to do this and which all are compatible with YUI.

1

There are 1 answers

0
barnyr On

I'm not aware of YUI3 providing exactly what you're after out-of-the box.

You can split your question into two parts:

Capturing errors

You either wrap your code with try/catch blocks or use a global error handler. It looks like YUI3 doesn't yet directly handle this (http://yuilibrary.com/projects/yui3/ticket/2528067) but handling it shouldn't be too hard, you'll just have to test for browser differences.

Sending Error data to the server

You ought to be able to use Y.IO to send back the error data to the server. It looks like you get errorMsg, url, lineNumber given to you, so you can just send them back to the server:

YUI().use("io-base",function(Y){

window.onerror = function(errorMsg, url, lineNumber){
  Y.io("/errorHandler.php", {
    data: {
        errorMsg: errorMsg,
        url: url,
        lineNumber: lineNumber
    }
});
alert("Sorry, something bad happened");
};
console.log("handler registered");

    //now trigger an error
    a.b.c="banana";

});

That seems to work here: http://jsfiddle.net/J83LW/

I'l leave the customized alert to you, I've left an alert here as a basic example of handling this