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

safe-try-with-ai

v1.5.1

Published

Clean error handling for JavaScript

Readme


safe-try-with-ai

A lightweight JavaScript utility for clean error handling with optional AI-style runtime suggestions, without repetitive try/catch blocks.


Installation

npm install safe-try-with-ai

Usage

Synchronous example

const { safeTry } = require("safe-try-with-ai");

const [err, result] = safeTry(() => JSON.parse('{"x":1}'));

if (err) {
  console.error(err);
} else {
  console.log(result); // { x: 1 }
}

Asynchronous example

const { safeTryAsync } = require("safe-try-with-ai");

const [err, data] = await safeTryAsync(async () => {
  return await fetchData();
});

if (err) {
  console.error(err);
}

Optional AI Runtime Suggestions

Enable AI-style runtime suggestions by passing { analyze: true } as the second argument.

const { safeTry } = require("safe-try-with-ai");

const [err] = safeTry(() => JSON.parse("invalid"), { analyze: true });

if (err) {
  console.error("Error:", err.message);
  console.log("Suggestion:", err.suggestion);
  console.log("Fix:", err.fix);
}

AI-style suggestions are rule-based and local. No real AI model, no network calls, no data collection.


Default Fallback Value

Use safeTryDefault to return a fallback value when an error occurs.

const { safeTryDefault } = require("safe-try-with-ai");

const result = safeTryDefault(
  () => JSON.parse("invalid"),
  {},
  { analyze: true }
);

console.log(result); // {}

Safe JSON Parsing

const { safeTryJson } = require("safe-try-with-ai");

const [err, data] = safeTryJson('{"x":1}', { analyze: true });

if (err) {
  console.error(err);
}

CLI Usage

Validate JSON files from the terminal:

npx safe-try-with-ai example.json
npx safe-try-with-ai example.json --analyze

Pipe JSON via stdin

cat example.json | npx safe-try-with-ai --stdin
cat bad.json | npx safe-try-with-ai --stdin --analyze

STDIN support (v1.5.0+)

Validate JSON from pipes:

cat file.json | safe-try-with-ai --stdin -a

CLI Output Legend

  • ✔ JSON valid (green)
  • ✖ JSON invalid (red)
  • Suggestions (blue)
  • Fix (green)

Exit Codes

  • 0 = valid JSON
  • 1 = invalid JSON or runtime error

TypeScript Support

Built-in TypeScript definitions included:

import { safeTry } from "safe-try-with-ai";

const [err, result] = safeTry(() => JSON.parse(data));

No configuration required.


Features

  • Works with synchronous and asynchronous functions
  • Eliminates repetitive try/catch blocks
  • Optional AI-style runtime suggestions
  • Default fallback handling
  • Safe JSON parsing helper
  • CLI support via npx and --stdin
  • Built-in TypeScript definitions
  • Zero dependencies
  • Lightweight and fast

Changelog

v1.5.1

  • Fixed CLI execution issues
  • Added short -a flag
  • Improved stdin handling
  • Reduced npm package size

v1.5.0

  • Added --stdin support for piping JSON to CLI
  • CLI improvements for clearer output with colors and symbols

v1.4.0

  • Enhanced CLI with colors and symbols for valid/invalid JSON
  • Improved AI-style suggestions formatting

v1.3.2

  • Bug fixes and small improvements

v1.3.1

  • Minor CLI fixes

v1.3.0

  • Added TypeScript definitions
  • Added CLI support
  • Documentation improvements

v1.2.0

  • Added safeTryDefault
  • Added safeTryJson
  • Improved AI-style suggestions

v1.1.1

  • Improved JSON error suggestions

License

MIT