Created
August 20, 2018 14:41
-
-
Save enkot/45bb2e4249d87c6bf274838bcf734a16 to your computer and use it in GitHub Desktop.
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
export const catchDecoratorStore = { | |
handler: null, | |
setHandler(handler) { | |
this.handler = handler | |
} | |
} | |
function Catch(target, key, descriptor) { | |
const originalMethod = descriptor.value | |
descriptor.value = async function(...args) { | |
try { | |
return await originalMethod.apply(this, args) | |
} catch (error) { | |
const { handler } = catchDecoratorStore | |
if (handler && typeof handler === 'function') | |
return handler() | |
console.warn(error.message) // default handler | |
} | |
} | |
return descriptor | |
} | |
export default Catch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment