Last active
April 16, 2016 02:58
-
-
Save ernestlv/a5d9d32c01bbbd57442a795cc58286f7 to your computer and use it in GitHub Desktop.
wraps the given function p in a try catch so is safer to call it in some contexts. e.g. analytic functions in unit tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
wraps the given function p in a try catch | |
so is safer to call it in some contexts. e.g. analytic functions in unit tests. | |
*/ | |
Object.prototype.try = function (p){ | |
var fn = function(){}; | |
if (typeof this[p] === 'function'){ | |
fn = function(){ | |
try { | |
this[p].apply(this, [].slice(arguments)); | |
}catch(e){ | |
console.error('catch:', this['__id__'] || '', p, e); | |
} | |
}.bind(this); | |
} | |
return fn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment