-
-
Save tugayilik/f574827ef31caf61b92de79b64dfa47e to your computer and use it in GitHub Desktop.
This file contains 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
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