@thinhbh/snippet
v2.1.13
Published
- Using Catch and CatchAll decorator to wrap method or class with try catch with an error and handler
Downloads
8
Readme
snippet
- Using Catch and CatchAll decorator to wrap method or class with try catch with an error and handler
Class
@Catch(Error, handlerFunction)
Class A{
method(){
throw new Error()
}
}
function handlerFunction(error: Error, ctx){
console.log(error)
}
- Using withTryCatch to wrap a function
function divide(a: number, b: number): number {
if (b === 0) {
throw new Error("Cannot divide by zero");
}
return a / b;
}
const safeDivide = withTryCatch(divide);
console.log(safeDivide(10, 2)); // 5
console.log(safeDivide(10, 0)); // Error: Cannot divide by zero
with class
with abstractclass
https://www.npmjs.com/package/@thinhbh/snippet