tryError Documentation
Installation
Get started with tryError in your TypeScript project
Package Installation
Note: tryError has zero runtime dependencies and works with TypeScript 4.5+
Performance: tryError adds <3% overhead to successful operations. Error handling overhead (20%-120%) is configurable based on your debugging needs. No configuration required to start!
Modular Imports for Smaller Bundles
📦 One Package, Multiple Entry Points:
You install the same @try-error/core
package regardless of which module you use. The different imports are just entry points within the package.
After installation, choose the import that best fits your needs. Hover over any code block below to copy the import statement:
Full Bundle (Default)
import { trySync, tryAsync, isTryError } from '@try-error/core';
Sync-Only Module
import { trySync, isTryError } from '@try-error/core/sync';
Async-Only Module
import { tryAsync, isTryError } from '@try-error/core/async';
Core Module
import { isTryError, createError } from '@try-error/core/core';
Tree-shaking: Modern bundlers (webpack, rollup, esbuild, vite) will automatically eliminate unused code when using modular imports.
TypeScript Configuration
For the best experience, ensure your tsconfig.json
has strict mode enabled:
{
"compilerOptions": {
"strict": true,
"exactOptionalPropertyTypes": true,
"noUncheckedIndexedAccess": true
}
}
Basic Usage
Import the functions you need and start handling errors safely:
1import { trySync, tryAsync, isTryError } from '@try-error/core';
2
3// Synchronous operations
4const result = trySync(() => JSON.parse(jsonString));
5if (isTryError(result)) {
6 console.error('Parse failed:', result.message);
7} else {
8 console.log('Parsed data:', result);
9}
10
11// Asynchronous operations
12const asyncResult = await tryAsync(() => fetch('/api/data'));
13if (isTryError(asyncResult)) {
14 console.error('Fetch failed:', asyncResult.message);
15} else {
16 const data = await asyncResult.json();
17}