Skip to content

Instantly share code, notes, and snippets.

@tugayilik
Created December 1, 2018 21:58
Show Gist options
  • Save tugayilik/f574827ef31caf61b92de79b64dfa47e to your computer and use it in GitHub Desktop.
Save tugayilik/f574827ef31caf61b92de79b64dfa47e to your computer and use it in GitHub Desktop.
function tryCatchProxy (superClass) {
const prototype = superClass.prototype;
if (Object.getOwnPropertyNames(prototype).length < 2) {
return superClass;
}
const handler = fn => () => {
try {
// Return is required for exposing result of execution
return fn.apply(this, arguments);
} catch (error) {
// Your catch logic. For example, log to database or send email.
console.log(error);
}
};
for (const property in Object.getOwnPropertyDescriptors(prototype)) {
if (prototype.hasOwnProperty(property) && property !== 'constructor' && typeof prototype[property] === 'function') {
superClass.prototype[property] = handler(superClass.prototype[property]);
}
}
return superClass;
}
class Person {
constructor () {
this.name = 'John';
this.surname = 'Doe';
}
get fullName () {
return `${this.name} ${this.surname}`;
}
get reverseName () {
return this.fullName.split('').reverse().join('');
}
addHelloPrefix () {
this.name = `Hello, ${this.reverseName}`;
// A undefined exception will be handled in proxy
f;
return this.name;
}
}
var person = new (tryCatchProxy(Person));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment