A lightweight, type-safe try/catch wrapper for handling async operations in TypeScript. Simplifies error handling by providing a consistent, type-safe way to handle both success and error cases.
npm install @clevali/trycatchA lightweight, type-safe try/catch wrapper for handling async operations in TypeScript. Simplifies error handling by providing a consistent, type-safe way to handle both success and error cases.
- đ¯ Type-safe error handling
- đĒļ Zero dependencies
- đĢ Simple, intuitive API
- đ Full TypeScript support
- đĻ ESM and CommonJS support
``bash`
npm install @clevali/trycatch
`typescript
import { tryCatch } from "@clevali/trycatch";
const result = await tryCatch(sampleFunction());
const { error, data } = result;
`
`typescript
import { tryCatch } from "@clevali/trycatch";
import axios from "axios";
// sample function that returns a promise
async function sampleFunction() {
const result = await tryCatch(axios.post(https://github.com/octocat));
if (result.error) {
throw {
code: result.error.code || 500,
message: result.error.message,
};
}
return "Hello, world!";
}
// main function
async function main() {
const result = await tryCatch(sampleFunction());
if (result.error) {
console.log(result.error.code); // 400
console.log(result.error.message); // "Bad Request"
cosole.log(result.error.stack); // stack trace
}
console.log(result.data); // "Hello, world!"
}
`
The package provides standardized error messages for common HTTP status codes. For example:
Common HTTP status codes are automatically mapped to their standard messages:
```
- 400: "Bad Request"
- 401: "Unauthorized"
- 403: "Forbidden"
- 404: "Not Found"
- 405: "Method Not Allowed"
- 408: "Request Timeout"
- 409: "Conflict"
- 429: "Too Many Requests"
- 500: "Internal Server Error"
- 502: "Bad Gateway"
- 503: "Service Unavailable"
- 504: "Gateway Timeout"