catch-wrap
v1.0.1
Published
Type-safe try-catch utility for TypeScript
Downloads
11
Maintainers
Readme
Catch Wrap
Type-safe error handling for sync & async TypeScript operations, no try-catch boilerplate
Features
- Type-safe: Full TypeScript support with proper type inference
- Universal: Works with sync functions, async functions, and promises
- Zero dependencies: Lightweight with no external dependencies
- Consistent API: Same interface for all operation types
- Promise-like support: Works with Drizzle, Prisma, and other thenable objects
Installation
pnpm add catch-wrap
# or
bun add catch-wrap
yarn add catch-wrap
npm install catch-wrapBasic Usage
Async Operations
import { tryCatch } from 'catch-wrap';
const { data, error } = await tryCatch(fetch('/api/users'));
if (error) {
console.error('Failed to fetch users:', error.message);
return;
}
console.log('Status:', data.status);
const { data, error } = await tryCatch(async () => {
const response = await fetch('/api/users');
if (!response.ok) throw new Error('API request failed');
return response.json();
});
if (error) {
console.error('Operation failed:', error.message);
} else {
console.log('Users:', data);
}Synchronous Operations
import { tryCatch } from 'catch-wrap';
const { data, error } = tryCatch(() => JSON.parse(jsonString));
if (error) {
console.error('Invalid JSON:', error.message);
return;
}
console.log('Parsed data:', data);Framework Integration
Database Operations (Drizzle, Prisma)
Perfect for database operations that return promise-like objects:
import { tryCatch } from 'catch-wrap';
const { data: user, error } = await tryCatch(
db.select().from(users).where(eq(users.id, userId))
);
const { data: users, error } = await tryCatch(
prisma.user.findMany({ where: { active: true } })
);
if (error) {
console.error('Database query failed:', error.message);
return [];
}
return users;File System Operations
import fs from 'fs/promises';
import { tryCatch } from 'catch-wrap';
const { data: fileContent, error } = await tryCatch(
fs.readFile('./config.json', 'utf8')
);
if (error) {
console.error('Failed to read config file:', error.message);
return;
}
const { data: config, error: parseError } = tryCatch(() =>
JSON.parse(fileContent)
);API Reference
tryCatch<T, E>(operation)
A universal error handling function that wraps operations and returns a result object.
Parameters:
operation: Can be one of:- A
Promise<T> - A function
() => Tthat might throw - A function
() => Promise<T>that returns a promise
- A
Returns:
- For sync operations:
Result<T, E> - For async operations:
Promise<Result<T, E>>
Type Parameters:
T: The expected return type of the successful operationE: The error type (defaults toError)
Types
type Result<T, E = Error> = Success<T> | Failure<E>;
type Success<T> = {
data: T;
error: null;
};
type Failure<E> = {
data: null;
error: E;
};Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.
