
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
trycatch-lib
Advanced tools
A utility to replace try-catch blocks with a tuple based error handling pattern
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;
}
any requiredTryCatchError// 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);
}
trycatch(fn) → Returns an async function that returns [result, error].TryCatchError → Custom error with .originalError and .timestamp.TryCatchError – Error Handling Made ConsistentAll errors returned by trycatch are wrapped in a TryCatchError instance for consistent, type-safe error handling.
TryCatchError?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
}
}
.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 eventspnpm test
MIT
FAQs
A utility to replace try-catch blocks with a tuple based error handling pattern
We found that trycatch-lib demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.