Package Exports
- soft-error
- soft-error/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (soft-error) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
soft-error
Simple TypeScript utilities for handling async/sync operations with built-in error management.
Installation
npm install soft-error
# or
yarn add soft-errorUsage
import { _try, tryCatch } from 'soft-error';_try
Run a handler (Promise or function), optionally handle errors, returns null on failure.
// async handler with custom error logging
const data = await _try(
() => fetch('/api/data').then(r => r.json()),
err => console.error('Fetch failed:', err.message)
);
// data: parsed JSON or null// sync handler without error handler
const result = await _try(() => computeValue());
// result: number or nulltryCatch
Run a handler, always returns an object { value, error, ok }.
const { value, error, ok } = await tryCatch<Error, number>(() => {
if (Math.random() < 0.5) throw new Error('Bad luck');
return 100;
});
if (ok) {
console.log('Got', value);
} else {
console.error('Error occurred:', error);
}// async example
const userResult = await tryCatch<Error, User>(async () => api.getUser(1));
if (!userResult.ok) {
console.warn('Could not fetch user:', userResult.error);
}API
type FnHandler<T> = Promise<T> | (() => T | Promise<T>);
type ErrorHandler<E extends Error> = (err: E) => void | Promise<void>;
async function _try<E extends Error, R>(
fn: FnHandler<R>,
onError?: ErrorHandler<E>
): Promise<R | null>;
interface TryCatchResult<E extends Error, R> {
value: R | null;
error: E | null;
ok: boolean;
}
async function tryCatch<E extends Error, R>(
fn: FnHandler<R>
): Promise<TryCatchResult<E, R>>;License
MIT © RYN BSD