npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

soft-error

v3.0.0

Published

Lightweight TypeScript utilities for safe async/sync error handling

Readme

soft-error

A tiny TypeScript utility to wrap synchronous or asynchronous functions in safe try/catch logic, returning null (for _try) or a structured result (for _catch) instead of throwing.

Features

  • _try: Executes code, catches errors or promise rejections, and returns either the result or null.
  • _catch: Executes code, catches errors or promise rejections, and returns { value, error, ok } for easier pattern matching.
  • Works seamlessly with both sync and async handlers.
  • Optional per-call error callbacks.

Installation

npm install soft-error
# or
yarn add soft-error

Usage

import { _try, _catch } from "soft-error";

_try

Signature

// Sync
function _try<T, E extends Error = Error>(opts: {
  handler: () => T;
  onError?: (err: E) => void;
}): T | null;

// Async
function _try<T, E extends Error = Error>(opts: {
  handler: () => Promise<T>;
  onError?: (err: E) => void;
}): Promise<T | null>;
  • handler: Your function (sync or async).
  • onError (optional): A callback invoked with the caught error; return value is ignored.

Returns the handler’s return value, or null if any exception/rejection occurred.

Examples
// Sync example
const result = _try({
  handler: () => JSON.parse('{"foo": 1}'),
  onError: (err) => console.error("Failed to parse:", err),
});
// result === { foo: 1 }

// Sync with error
const bad = _try({
  handler: () => JSON.parse("not valid"),
  onError: (err) => console.warn("Parse error"),
});
// bad === null

// Async example
(async () => {
  const data = await _try({
    handler: () => fetch("/api/data").then((r) => r.json()),
    onError: (err) => alert("Network error"),
  });
  // data is parsed JSON or null
})();

_try

Signature

// Sync
function _catch<T, E extends Error = Error>(
  handler: () => T
): { value: T; error: null; ok: true } | { value: null; error: E; ok: false };

// Async
function _catch<T, E extends Error = Error>(
  handler: () => Promise<T>
): Promise<
  { value: T; error: null; ok: true } | { value: null; error: E; ok: false }
>;

Returns an object:

  • ok: true on success, with value: T and error: null.
  • ok: false on failure, with value: null and error: E.
Examples
// Sync success
const result = _catch(() => 2 + 2);
// => { ok: true, value: 4, error: null }

// Sync failure
const failed = _catch(() => {
  throw new TypeError("oops");
});
// => { ok: false, value: null, error: TypeError("oops") }

// Async success
(async () => {
  const res = await _catch(() => Promise.resolve("hello"));
  // => { ok: true, value: "hello", error: null }
})();

// Async failure
(async () => {
  const res = await _catch(() => Promise.reject(new Error("fail")));
  // => { ok: false, value: null, error: Error("fail") }
})();

API Reference

| Function | Returns | Description | | ---------------- | --------------------------------------------- | --------------------------------------------------- | | isPromise(obj) | obj is Promise<T> | Type-guard to detect Promise-like values. | | _try(opts) | T \| nullorPromise<T \| null> | Execute handler safely, return null on error. | | _catch(fn) | CatchResult<T, E> or Promise<CatchResult> | Execute handler, return structured success/failure. |

License

MIT © Rayen Boussayed