javascript - Logging client-side errors and "Script error" -


how pinpoint client-side errors occur in scripts domains?

for clarity let's suppose have average size web application uses several scripts hosted domains (like google maps js sdk).

and 1 day started receiving script error in error logs, means there error occurred in 3rd party code.

but how find exact method in code invoked 3rd party code failed?

ps: error not reproducible developers , occurs on client machines quite rarely.

pps: errors above window.onerror does not provide call stack, proper error message, file name , line number. provides literally nothing helpful apart of script error error message.

ppps:

the third party script included using <script src="http://..."></script> tags , executed using somefunctionfromthethirdpartyscript();

i had similar problem once , solution was...

//  parameters automatically passed window.onerror handler... function myerrorfunction(message, url, linenumber) {     $.post(         "https://host.and/path/to/script/that/stores/entries",         {             "url":url, //  url of page error occured             "linenumber":linenumber, //  line number error occer             "message":message  //error message         },         function(){             //callback function $.post()             if(console)                 if(console.log)                     console.log("error reported.");         }     ); } window.onerror = myerrorfunction;  //adds  function "myerrorfunction" onerror event 

to more sophisticated need utilize tricks have put debug project at: https://github.com/luke80/javascript-debugtools-luke

edit: ok, i'll gather out of project important bits apply problem:

/*   string prototype .hashcode()   from: http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery */ if(typeof string['hashcode'] == "undefined") {     string.prototype.hashcode = function(){     var hash = 0, i, char;     if (this.length == 0) return hash;     (i = 0, l = this.length; < l; i++) {         char  = this.charcodeat(i);         hash  = ((hash<<5)-hash)+char;         hash |= 0; // convert 32bit integer     }     return hash; }; //  start of vars var _log_callerargs_on = true,     getcallerhash = function(funcstring) {         return callerfunc.tostring().hashcode();     },     getcallerargs = function(obj) {         return json.stringify(array.prototype.slice.call(obj),this._detectcircularity(array.prototype.slice.call(obj))).replace(/^\[/,"(").replace(/\]$/,")");     },     detectcircularity = function(obj) {  //  from: http://stackoverflow.com/questions/4816099/chrome-sendrequest-error-typeerror-converting-circular-structure-to-json         return (function() {             var = 0;             return function(key, value) {                 if(i !== 0 && typeof(obj) === 'object' && typeof(value) == 'object' && obj == value) return '[circular]';                  if(i >= 29) return '[too deep, not mined]';                 ++i;                 return value;               }         })(detectcircularity);     },     caller = this.o.caller || arguments.callee.caller || "top"; //  end of vars if(typeof caller != "string") {     if(caller) {         var callerdata = ((caller.name)?caller.name:"unnamed caller:"+getcallerhash(caller))+((_log_callerargs_on)?getcallerargs(caller.arguments):"");         //  since loop become problematic (infinite loop, anyone?) lets impose limit.         var maxloops = 64;         var loopcounter = 0;         //  gather (or 64 of them) caller names (and optionally parameters)         while(caller.caller && loopcounter < maxloops) {  //  <--- there error here fixed on oct 15, 2013 @ 11:55am             callerdata += " <- "+((caller.caller.name)?caller.caller.name:"unnamed caller:"+getcallerhash(caller.caller))+((_log_callerargs_on)?getcallerargs(caller.caller.arguments):"")             caller = caller.caller;             loopcounter++;         }         // callerdata populated stack trace.     } else {         // can't errors non-existent caller     } } 

the callerdata variable should populated string of function names (or hash of function contents can semi-identify them) optionally parameters functions called with.

while can't function names, contents of functions can identified (as should stay same) , can still useful-ish debug information parameters passed, etc.

note: didn't test code above, should same code repo. if doesn't work, please refer repo , re-factor code there needs. :)

i hope helps.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -