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

compound-errors

v0.1.2

Published

A simple HOC utility to attach error classes to functions and class methods at both typescript type system and runtime.

Readme

Compound Errors

A TypeScript utility that enables type-safe error handling by attaching error classes to functions and class methods at both compile-time and runtime.

Why Compound Errors?

In traditional JavaScript/TypeScript error handling, you often encounter these problems:

  1. Unclear error types: Functions can throw various errors, but there's no clear indication of what errors to expect
  2. Runtime surprises: You might miss handling certain error cases because they're not explicitly documented
  3. Poor developer experience: No autocomplete or type checking for error handling

Compound Errors solves these issues by:

  • Explicit error declaration: Clearly define what errors a function/method can throw
  • Type safety: Get compile-time checking and autocomplete for error handling
  • Runtime access: Access error constructors directly from the function/method for consistent error throwing and handling

Installation

npm install compound-errors
# or
pnpm add compound-errors
# or
yarn add compound-errors

Usage

Function Error Annotation

Annotate standalone functions with their possible errors:

import { withErrors } from 'compound-errors';

// Define custom error classes
class ValidationError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'ValidationError';
  }
}

class NetworkError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'NetworkError';
  }
}

// Annotate function with possible errors
const fetchUserData = withErrors(
  async (userId: string) => {
    if (!userId) {
      throw new fetchUserData.ValidationError('User ID is required');
    }
    
    try {
      const response = await fetch(`/api/users/${userId}`);
      return response.json();
    } catch {
      throw new fetchUserData.NetworkError('Failed to fetch user data');
    }
  },
  {
    ValidationError,
    NetworkError,
  }
);

// Usage with type-safe error handling
try {
  const user = await fetchUserData('123');
} catch (error) {
  if (error instanceof fetchUserData.ValidationError) {
    // Handle validation error - TypeScript knows this is ValidationError
    console.error('Validation failed:', error.message);
  } else if (error instanceof fetchUserData.NetworkError) {
    // Handle network error - TypeScript knows this is NetworkError
    console.error('Network error:', error.message);
  }
}

Class Method Error Annotation

Annotate class methods with their possible errors:

import { withErrors } from 'compound-errors';

class AuthenticationError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'AuthenticationError';
  }
}

class DatabaseError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'DatabaseError';
  }
}

class UserService {
  async login(username: string, password: string) {
    // Login implementation
  }
  
  async createUser(userData: object) {
    // Create user implementation
  }
  
  async deleteUser(userId: string) {
    // Delete user implementation
  }
}

// Annotate class with method-specific errors
const TypedUserService = withErrors(UserService, {
  login: {
    AuthenticationError,
    DatabaseError,
  },
  createUser: {
    ValidationError,
    DatabaseError,
  },
  deleteUser: {
    DatabaseError,
  },
});

// Usage
const userService = new TypedUserService();

try {
  await userService.login('john', 'password123');
} catch (error) {
  if (error instanceof userService.login.AuthenticationError) {
    // Handle authentication error
    console.error('Login failed:', error.message);
  } else if (error instanceof userService.login.DatabaseError) {
    // Handle database error
    console.error('Database error during login:', error.message);
  }
}
import { withErrors } from 'compound-errors';

const UserService = withErrors(class UserService {
  login(username: string, password: string) {
    // Login implementation
    throw new AuthenticationError('Invalid username or password');
    // ...
  }
  createUser(userData: object) {
    // Create user implementation
  }
  deleteUser(userId: string) {
    // Delete user implementation
  }
}, {
  login: {
    AuthenticationError,
    DatabaseError,
  },
});

Benefits

1. Type Safety

Get compile-time checking and IntelliSense support for error handling:

// TypeScript will suggest available error types
if (error instanceof fetchUserData.ValidationError) {
  // ✅ TypeScript knows this is ValidationError
}

// TypeScript will warn about typos
if (error instanceof fetchUserData.ValidatonError) {
  // ❌ TypeScript error: Property 'ValidatonError' does not exist
}

2. Self-Documenting Code

Functions and methods clearly declare what errors they can throw:

// Just by looking at the function signature, you know what errors to expect
const processPayment = withErrors(originalFunction, {
  PaymentError,
  ValidationError,
  NetworkError,
});

3. Consistent Error Handling

Access error constructors directly from the function for consistent error creation:

// Always use the attached error classes
throw new processPayment.PaymentError('Insufficient funds');

// Rather than creating errors separately
throw new PaymentError('Insufficient funds'); // Less clear relationship

4. Better Developer Experience

  • Autocomplete for available error types
  • Clear error hierarchy and relationships
  • Reduced runtime surprises

API Reference

withErrors(fn, errors)

Annotates a function with error types.

Parameters:

  • fn: The function to annotate
  • errors: Object mapping error names to error constructor classes

Returns: The original function with error constructors attached as properties

withErrors(BaseClass, errorConfig)

Annotates a class with method-specific error types.

Parameters:

  • BaseClass: The class constructor to annotate
  • errorConfig: Object mapping method names to their error configurations

Returns: The original class with error constructors attached to method properties

TypeScript Support

This library is built with TypeScript and provides full type safety. The error annotations are preserved in the type system, enabling:

  • Compile-time error checking
  • IntelliSense and autocomplete
  • Type-safe error handling patterns

License

MIT