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

@spongesoftware/trycatch

v1.0.1

Published

simplify try catch handling in JS/TS

Readme

tryCatch.js Logo

@spongesoftware/trycatch

npm version Build Status GitHub License GitHub branch check runs GitHub Issues or Pull Requests

Simplify try catch handling in JS/TS with explicit error management using discriminated union types.

🚀 Installation

Install with npm:

npm install @spongesoftware/trycatch

Or with yarn:

yarn add @spongesoftware/trycatch

Or with pnpm:

pnpm add @spongesoftware/trycatch

📋 Requirements

  • Node.js: >=20.0.0
  • npm: >=9.0.0
  • pnpm: >=8.0.0

🛠️ Features

  • TypeScript support with full type safety
  • Dual module support (CommonJS and ESM)
  • Zero dependencies (runtime)
  • Explicit error handling without throwing exceptions
  • Discriminated union types for type-safe error management
  • Support for both sync and async operations

📖 Usage

Both tryCatch (for Promises) and tryCatchSync (for synchronous functions) return a Promise<Result> or Result object respectively. The Result type is a discriminated union with three properties: data, error, and success.

  • If the operation succeeds: success will be true, data will contain the result, and error will be null.
  • If the operation fails: success will be false, error will contain the caught exception, and data will be null.

This pattern allows for explicit error handling without throwing exceptions.

tryCatch: For Asynchronous Operations (Promises)

Wraps an async operation in a try-catch block and returns a Promise<Result>.

import { tryCatch } from "@spongesoftware/trycatch"

// Example: TypeScript with explicit generics
interface User {
  id: number
  name: string
}

async function fetchUserById(userId: number) {
  const { data, error, success } = await tryCatch<User, Error>(
    fetch(`https://api.example.com/users/${userId}`).then((res) => {
      if (!res.ok) {
        throw new Error(`HTTP error! status: ${res.status}`)
      }
      return res.json()
    })
  )

  if (success) {
    console.log("User fetched:", data.name) // data is of type User
    return data
  } else {
    console.error("Failed to fetch user:", error.message) // error is of type Error
    return null
  }
}

fetchUserById(123)

// Example: Inferred types for a simple API call
async function runAsyncExample() {
  const { data, error, success } = await tryCatch(async () => {
    await new Promise((resolve) => setTimeout(resolve, 100))
    if (Math.random() > 0.5) {
      throw new Error("Asynchronous operation failed")
    }
    return "Asynchronous success!"
  })

  if (success) {
    console.log("Async operation data:", data)
  } else {
    console.error("Async operation error:", error.message)
  }
}

runAsyncExample()

tryCatchSync: For Synchronous Operations

Wraps a synchronous operation in a try-catch block and returns a Result object directly.

import { tryCatchSync } from "@spongesoftware/trycatch"

// Example: TypeScript with explicit generics
interface AppConfig {
  apiUrl: string
  timeoutMs: number
}

const configString = '{"apiUrl": "/api", "timeoutMs": 5000}'

const { data, error, success } = tryCatchSync<AppConfig, SyntaxError>(() => {
  const parsed = JSON.parse(configString)
  if (typeof parsed.timeoutMs !== "number") {
    throw new SyntaxError("timeoutMs must be a number")
  }
  return parsed as AppConfig
})

if (success) {
  console.log("Config loaded:", data.apiUrl, data.timeoutMs) // data is of type AppConfig
} else {
  console.error("Failed to parse config:", error.message) // error is of type SyntaxError
}

// Example: Inferred types
const jsonString = '{"value": 123}'

const { data: parsedData, error: parseError, success: parseSuccess } =
  tryCatchSync(() => JSON.parse(jsonString))

if (parseSuccess) {
  console.log("Parsed synchronous data:", parsedData)
} else {
  console.error("Synchronous parse error:", parseError.message)
}

📚 API Reference

tryCatch<T, E>(promise: Promise<T>): Promise<Result<T, E>>

Wraps a Promise in a try-catch block and returns a Promise that resolves to a Result.

Type Parameters:

  • T - The type of the successful result (defaults to unknown)
  • E - The type of the error (defaults to Error)

Parameters:

  • promise - The Promise to execute and handle

Returns: A Promise that resolves to either a Success or Failure result

tryCatchSync<T, E>(callback: () => T): Result<T, E>

Wraps a synchronous function in a try-catch block and returns a Result.

Type Parameters:

  • T - The type of the successful result (defaults to unknown)
  • E - The type of the error (defaults to Error)

Parameters:

  • callback - The synchronous function to execute and handle

Returns: Either a Success or Failure result

Result<T, E>

A discriminated union type representing either a successful or failed operation.

Success Type:

interface Success<T> {
  data: T
  error: null
  success: true
}

Failure Type:

interface Failure<E> {
  data: null
  error: E
  success: false
}

🤝 Contributing

Contributions are welcome! Please feel free to open an issue or submit a pull request.

👨‍💻 Author

Andrew Brunker - [email protected]

📄 License

This project is licensed under the Apache-2.0 License - see the LICENSE.md file for details.