Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@andreasnicolaou/safe

andreasnicolaou15MIT1.0.0TypeScript support: included

A simple utility for handling synchronous and asynchronous errors without relying on try-catch blocks.

retry, timeout, exponential backoff, error handling, asynchronous, synchronous, promise, rxjs, typescript, utility, safe, async utilities, error boundaries

readme

@andreasnicolaou/safe — Say Goodbye to Try-Catch Blocks!

Tired of writing try...catch everywhere?

Let @andreasnicolaou/safe handle it for you. This tiny, library automatically wraps your functions and promises in a safe execution environment.

Features

  • Catch errors automatically (No more try...catch spam!)
  • Works for both synchronous & asynchronous functions
  • Customizable error logging

Installation

npm install @andreasnicolaou/safe

Basic Usage

import { safe, isSuccess, isFailure } from '@andreasnicolaou/safe';

const [error, result] = safe(() => {
  throw new Error('An error Occured!');
});

console.log(error); // An error Occured!
console.log(result); // undefined
console.log(isSuccess([error, result])); // false
console.log(isFailure([error, result])); // true

Advanced Error Handling

const { safe, safeWithRetries } = createSafeUtils({
  logger: (error) => sentry.captureException(error),
  logErrors: process.env.NODE_ENV === 'production',
});

Architecture Benefits

  • Functional Programming Friendly
    Compose operations without error handling noise
  • TypeScript Optimized
    Full type inference and guards
  • Framework Agnostic
    Works with React, Node.js, Deno, etc.

Contributing

Contributions are welcome! If you encounter issues or have ideas to enhance the library, feel free to submit an issue or pull request.