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

@theateros/failure

v0.0.2

Published

<p align="center"> <img src="../../.etc/assets/failure-logo.webp" alt="Theater OS - Foundations - Failure"> </p>

Readme

Theater OS - Failure

A domain-specific error class that extends the native Error class with additional properties and utilities to help with error handling in TypeScript applications.

Why Failure?

Error handling is a critical aspect of building robust applications, but JavaScript's native Error class has limitations:

  • No type safety: It's hard to distinguish between different types of errors at compile time
  • No structured error data: Errors often need additional context beyond just a message
  • Inconsistent error handling: Different parts of your codebase may handle errors differently
  • No easy way to create domain-specific errors: You have to manually extend Error for each error type

Failure addresses these issues by providing:

  • Type-safe error handling: Use TypeScript's type system to ensure you're handling the right errors
  • Structured error data: Attach additional context through the options parameter
  • Named failures: Create domain-specific error types without boilerplate
  • Consistent API: All error operations use a unified, static method API
  • Better stack traces: Automatic stack trace capture for better debugging

Installation

npm install @theateros/failure

Getting Started

Basic Usage

import { Failure } from '@theateros/failure'

// Create a simple failure
const error = Failure.of('Something went wrong')

// Create a failure with additional context
const errorWithCause = Failure.of('Operation failed', {
  cause: new Error('Network timeout')
})

// Throw the failure
throw error

Creating Named Failures

Named failures allow you to create domain-specific error types:

import { Failure } from '@theateros/failure'

// Create a named failure class
const NotFoundFailure = Failure.named('NotFoundFailure')
const ValidationFailure = Failure.named('ValidationFailure')

// Use them
throw new NotFoundFailure('User not found')
throw new ValidationFailure('Invalid email format')

Passing Options and Cause

You can pass additional options and a cause to failures for better error context:

import { Failure } from '@theateros/failure'

// Create a failure with a cause
const error = Failure.of('Operation failed', {
  cause: new Error('Network timeout')
})

// Create a named failure with options
const NotFoundError = Failure.named('NotFoundError')
throw new NotFoundError('User not found', {
  cause: new Error('Database query returned no results')
})

// Access the cause later
try {
  // some operation
} catch (error) {
  if (Failure.is(error) && error.cause) {
    console.log('Original error:', error.cause)
    console.log('Failure message:', error.message)
  }
}

Type Guards

Use type guards to safely check error types:

import { Failure } from '@theateros/failure'

try {
  // some operation
} catch (error) {
  if (Failure.is(error)) {
    console.log('Caught a failure:', error.message)
  }

  if (Failure.isNamed(error, 'NotFoundFailure')) {
    // TypeScript knows this is a NotFoundFailure
    console.log('Resource not found')
  }
}

API Reference

Failure Class

The main error class that extends Error.

Constructor

new Failure(message: string, options?: ErrorOptions)

Static Methods

  • Failure.named<N>(name: N): Creates a named failure class
  • Failure.of(message, options?): Creates a new failure instance
  • Failure.ofNamed(failure, message, options?): Creates a failure from a named failure class
  • Failure.is(error): Type guard to check if an error is a Failure
  • Failure.isNamed(error, name): Type guard to check if an error is a named failure
  • Failure.panic(error): Throws an error (functional style)

Properties

  • message: string: The error message
  • name: string: The error name (default: 'Failure')
  • options?: ErrorOptions: Additional error options
  • cause?: unknown: The cause of the error (from options)
  • stack?: string: The stack trace