Skip to content

Instantly share code, notes, and snippets.

@ernestlv
Last active April 16, 2016 02:58
Show Gist options
  • Save ernestlv/a5d9d32c01bbbd57442a795cc58286f7 to your computer and use it in GitHub Desktop.
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.
/*
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