try-catcher-ts
v0.1.1
Published
A type-safe try/catch wrapper
Readme
try-catcher-ts
A type-safe try/catch wrapper
Install
# npm
npm install try-catcher-ts
# pnpm
pnpm add try-catcher-ts
# yarn
yarn add try-catcher-ts
# bun
bun add try-catcher-tsUsage
There are 2 functions you can call, depending on if you'd like to wrap a syncronous or asyncronous function.
Async
For asyncronous functions, simply import the tryCatch() function. It takes any asyncronous function as the first argument, and, optionally, a string to display when an error is thrown, and either returns the result of your function, or throws an error.
import { tryCatch } from 'try-catcher-ts'
async function yourAsyncFunction(id: string) {
const res = await fetch(`https://example.com/api/item/${id}`)
return res
}
const response = await tryCatch(() => yourAsyncFunction('123'), 'Error fetching item')Sync
For syncronous functions, simply import the tryCatchSync() function. It takes any syncronous function as the first argument, and, optionally, a string to display when an error is thrown, and either returns the result of your function, or throws an error.
import { tryCatchSync } from 'try-catcher-ts'
function yourSyncFunction(name: string) {
const message = `Hello, ${name}!`
return message
}
const response = tryCatchSync(() => yourSyncFunction('Steve'), 'Error creating message')