How can I override/extend ReferenceError in Chrome's JavaScript? -


to make debugging easier, i'm capturing of console logs in chrome users submit feedback entry submit of logs our server. when encounters problem in production, can first , foremost them work can sit down , more thoroughly go through of logs determine root cause of whatever issue user encountered in production.

the technique use capture logs involves overriding console.log text entered in first argument gets stored in array while simultaneously invoking legacy function can still see logs in console too.

the problem when there's occasional uncaught exception. these aren't included in uploaded logs, it's not clear caused problem. tried overriding referenceerror writing javascript function takes function argument, returns new function stuff it, storing data in variable, , invoking legacy function last step:

function overrideexception(legacyfn) {        /** arguments original fn **/     return function() {          var args = [];          args[0] = arguments[0];          // pass in arguments original function , store result            // prove overrode referenceerror         output = ">> " + legacyfn.apply(this, args).stack;          return legacyfn.apply(this, arguments);     }             } 

to test overrideexception function, ran following code on console:

referenceerror = overrideexception(referenceerror); 

afterwards, tested returned function, new referenceerror, manually throwing referenceerror:

throw new referenceerror("yes!! works! hahaha!"); 

the resulting output on console is:

referenceerror: yes!! works! hahaha!

and checking global variable output overrideexception function shows did indeed run:

output   ">> referenceerror: yes!! works! hahaha!   @ referenceerror (<anonymous>)   @ new <anonymous> (<anonymous>:18:35)   @ <anonymous>:2:7   @ object.injectedscript._evaluateon (<anonymous>:562:39)   @ object.injectedscript._evaluateandwrap (<anonymous>:521:52)   @ object.injectedscript.evaluate (<anonymous>:440:21)" 

now, here's things start fall apart. in our code, we're not going know when uncaught exception occurs, tested attempting run function doesn't exist:

ttt(); 

which results in:

referenceerror: ttt not defined

however, unlike case explicitly throw error, in case, function doesn't fire, , we're left legacy functionality. contents of variable output same in first test.

so question seems this: how override referenceerror functionality javascript engine uses throw errors it's same 1 use when throw referenceerror?

keep in mind problem limited chrome @ time; i'm building chrome packaged app.

i have done quite bit of research same reason: wanted log errors , report them.

"overriding" native type (whether referenceerror, string, or array) not possible.

chrome binds these before javascript run, redefining window.referenceerror has no effect.

you can extend referenceerror referenceerror.prototype.extension = function() { return 0; }, or override tostring (for consistency, try on page, not dev tools).

that doesn't much.

but not worry....

(1) use window.onerror file name, 1-indexed line number, , 0-indexed position of uncaught errors, error itself.

var errordata = []; onerror = function(message, file, line, position, error) {     errordata.push({message:message, file:file, line:line, position:position, error:error}); }; 

see fiddle example. since op chrome-specific, has been tested work in chrome.

(2) because of improvements (1), no longer necessary, leave second technique here completeness, , since onerror not guaranteed work errors on browsers. see following:

var errors = []; function protectedfunction(f) {     return function() {         try {             f.apply(this, arguments);         } catch(e) {             errors.push(e);             throw e;         }     }; } settimeout = protectedfunction(settimeout); setinterval = protectedfunction(setinterval); etc... 

fyi, similar has been done in google closure compiler library, in goog.debug, created during gmail development intent of doing this. of particular interest goog.debug.errorhandler , goog.debug.errorreporter.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -