Simple, type-safe error handling for TypeScript.
pnpm add trycatch-lib
# or
yarn add trycatch-lib
# or
npm install trycatch-lib
async function fetchUser(id: string) {
try {
const res = await fetch(`/api/user/${id}`);
const data = await res.json();
return data;
} catch (err) {
return null; // Error details lost
}
}
import { trycatch } from "trycatch-lib";
async function fetchUser(id: string) {
// For built-in functions like fetch, pass directly
const [res, fetchErr] = await trycatch(fetch(`/api/user/${id}`));
if (fetchErr) return null;
// For custom logic, use an arrow/anonymous function is what i recommend
const [data, jsonErr] = await trycatch(() => {
// your custom logic
});
if (jsonErr) return null;
return data;
}
- 🧩 [result, error] tuple for all functions
- 🔄 Works with sync & async (Promise) functions
- 🧠 Fully type-safe, no
any
required - 🔍 Rich error info via
TryCatchError
- 🚀 Minimal, zero-config API
// For built-in or pre-made functions, pass directly: ( way i recommend)
const [result, error] = await trycatch(fetch(url));
// For custom logic, use an arrow/anonymous function i reccomend, it prevents you to make a additional wrappers
const [result, error] = await trycatch(() => {
// function body
});
if (error) {
// error is always a TryCatchError instance:
console.error(error.message);
}
- Use arrow/anonymous functions for custom logic.
- Pass built-in or pre-made functions (like fetch, JSON.parse) directly.
- Handles both sync and async functions.
trycatch(fn)
→ Returns an async function that returns[result, error]
.TryCatchError
→ Custom error with.originalError
and.timestamp
.
All errors returned by trycatch
are wrapped in a TryCatchError
instance for consistent, type-safe error handling.
A custom error class (see src/errors/TryCatchError.ts
) that standardizes error information, making it easy to inspect, log, or handle errors in a predictable way.
message: string
– Human-readable error message (from the original error, or a default fallback)originalError: unknown
– The original error value (can be any type:Error
, string, object, etc.)timestamp: number
– When the error was created (milliseconds since epoch)
import { trycatch, TryCatchError } from "trycatch-lib";
const [result, error] = await trycatch(() => {
// function logic which can throw error
return; // fn return type
});
if (error) {
// Always a TryCatchError instance
console.error("Message:", error.message);
console.error("Original error:", error.originalError);
console.log("Occurred at:", new Date(error.timestamp));
// How to use it as Type guard
if (error instanceof TryCatchError) {
// ...handle specifically
}
}
- Use
.originalError
if you need the raw error (for logging, rethrowing, etc.) .message
is always a string, even if the original error was not.timestamp
helps with debugging and tracing error events
pnpm test
MIT