@thinhbh/snippet
TypeScript icon, indicating that this package has built-in type declarations

2.1.13 • Public • Published

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
    image

  • with abstractclass
    image

https://www.npmjs.com/package/@thinhbh/snippet

Readme

Keywords

none

Package Sidebar

Install

npm i @thinhbh/snippet

Weekly Downloads

8

Version

2.1.13

License

none

Unpacked Size

24.2 kB

Total Files

22

Last publish

Collaborators

  • thinhbuihong