Skip to content

Instantly share code, notes, and snippets.

@enkot
Created August 20, 2018 14:41
Show Gist options
  • Save enkot/45bb2e4249d87c6bf274838bcf734a16 to your computer and use it in GitHub Desktop.
Save enkot/45bb2e4249d87c6bf274838bcf734a16 to your computer and use it in GitHub Desktop.
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