
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
@itsezz/try-catch
Advanced tools
A TypeScript utility for elegant error handling with Result types
A lightweight TypeScript utility for handling errors using Result types, making your code cleaner and more predictable.
npm install @itsezz/try-catch
# or
yarn add @itsezz/try-catch
# or
pnpm add @itsezz/try-catch
import { isError, isSuccess, tryCatch, tryCatchAsync, tryCatchSync, t, tc, tca } from '@itsezz/try-catch';
// Synchronous operations
const result = tryCatchSync(() => JSON.parse('{"name": "user"}'));
// Or using short alias:
const result2 = tc(() => JSON.parse('{"name": "user"}'));
if (isSuccess(result)) {
console.log(result.data.name); // "user"
} else {
console.error(result.error);
}
// Asynchronous operations
const asyncResult = await tryCatchAsync(async () => {
const response = await fetch('/api/user');
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return response.json();
});
// Or using short alias:
const asyncResult2 = await tca(async () => {
const response = await fetch('/api/user');
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return response.json();
});
if (isSuccess(asyncResult)) {
return asyncResult.data;
} else {
console.error(asyncResult.error);
return { name: 'Unknown' };
}
// Generic tryCatch (auto-detects sync/async)
const syncResult = tryCatch(() => 'hello world');
const asyncResult3 = await tryCatch(async () => 'hello async world');
// Or using short alias:
const syncResult2 = t(() => 'hello world');
const asyncResult4 = await t(async () => 'hello async world');
// Functional composition
import { map, flatMap, unwrapOr, match, success, failure } from '@itsezz/try-catch';
const result = unwrapOr(
map(
flatMap(
tc(() => JSON.parse('{"value": "42"}')), // Using short alias
data => tc(() => parseInt(data.value))
),
num => num * 2
),
0
);
console.log(result); // 84
// Complex error handling with pattern matching
const processUser = (jsonString: string) => {
const parseResult = tc(() => JSON.parse(jsonString));
const nameResult = flatMap(parseResult, data =>
data.name ? success(data.name) : failure('No name field')
);
const upperResult = map(nameResult, name => name.toUpperCase());
return match(upperResult, {
success: (name) => `Hello, ${name}!`,
failure: (error) => `Error: ${error}`
});
};
tryCatch<T, E>(fn: () => T): Result<T, E>
Executes a synchronous function, capturing any errors.
tryCatch<T, E>(fn: () => Promise<T>): Promise<Result<T, E>>
Executes an async function, capturing any errors.
tryCatch<T, E>(promise: Promise<T>): Promise<Result<T, E>>
Awaits a promise, capturing any errors.
tryCatchSync<T, E>(fn: () => T): Result<T, E>
Executes synchronous functions, guaranteeing sync Result return.
tryCatchAsync<T, E>(fn: () => Promise<T>): Promise<Result<T, E>>
Executes an async function, guaranteeing Promise return.
tryCatchAsync<T, E>(promise: Promise<T>): Promise<Result<T, E>>
Awaits a promise, guaranteeing Promise return.
Note:
tryCatch
may not correctly infer if the result isPromise<Result<T,E>>
orResult<T,E>
in certain conditions and defaults toPromise<Result<T,E>>
when unsure. Use explicit variants for guaranteed type safety.
t
- tryCatch
tc
- tryCatchSync
tca
- tryCatchAsync
isSuccess<T, E>(result: Result<T, E>): result is Success<T>
Type guard that narrows a result to Success type.
isError<T, E>(result: Result<T, E>): result is Failure<E>
Type guard that narrows a result to Failure type.
success<T>(data: T): Success<T>
Creates a success result with the given data.
failure<E>(error: E): Failure<E>
Creates a failure result with the given error.
Functional approach with excellent type inference:
map<T, U, E>(result: Result<T, E>, fn: (data: T) => U): Result<U, E>
Transforms the data of a successful result. If the result is a failure, returns the failure unchanged.
flatMap<T, U, E>(result: Result<T, E>, fn: (data: T) => Result<U, E>): Result<U, E>
Transforms the data using a function that returns a Result. Useful for chaining operations that might fail.
unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T
Extracts the data from a successful result or returns a default value for failures.
unwrapOrElse<T, E>(result: Result<T, E>, fn: (error: E) => T): T
Extracts the data from a successful result or computes a default value using the error.
match<T, E, U>(result: Result<T, E>, handlers: { success: (data: T) => U; failure: (error: E) => U }): U
Pattern matching for Result types - handles both success and failure cases in one expression.
Supports both ESM and CommonJS:
// ESM
import { tryCatch } from '@itsezz/try-catch';
// CommonJS
const { tryCatch } = require('@itsezz/try-catch');
MIT
FAQs
A TypeScript utility for elegant error handling with Result types
The npm package @itsezz/try-catch receives a total of 0 weekly downloads. As such, @itsezz/try-catch popularity was classified as not popular.
We found that @itsezz/try-catch 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.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.