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

simple-failure

v0.0.2

Published

["Maybe Not" - Rich Hickey](https://www.youtube.com/watch?v=YR5WdGrpoug)

Readme

Simple Failure

"Maybe Not" - Rich Hickey

Simple errors as values. Intended to be used in a union instead of bieng a wrapper. Typescript can do better than Result/Optional/Maybe.

How to use

Example function that can fail and handling the failure:

import { failure, isFailure } from 'simple-failure';

function safeSort(values: number[]): number[] | Failure<string> {
    if (values.includes(69)) {
        return failure("Naughty numbers aren't allowed");
    }
    return values.toSorted()
}

const sortedNumbers = safeSort([11, 22, 69, 12]);
if (isFailure(sortedNumbers)){
    console.log('error', getFailure(sortedNumbers));
    process.exit(1);
}
console.log(sortedNumbers);

Create Failure value:

import { failure } from 'simple-failure';

failure(new Error("something"));

Check if a value is a Failure:

import { isFailure } from 'simple-failure';

const result = doSomething(42);
if (isFailure(result)) {
    // handle failure
} else {
    // handle success
}

Throw the error inside the failure if the value is a failure:

import { throwFailure } from 'simple-failure';
const result = doSomething(42);
throwFailure(result)
// handle success

Get the error inside the failure if the value is a failure, returns undefined if it isn't:

import { getFailure } from 'simple-failure';
const result = doSomething(42);
const error = getFailure(result);
if (error) {
    console.log(error);
}

Wrap a block of code or a promise, and turn exceptions/rejections into Failures. Supports functions, async functions, and Promises

import { captureFailure, isFailure } from 'simple-failure';
const result = await captureFailure(async () => {
    const foo = await doSomething();
    const bar = await doSomethingElse();
    return foo + bar;
});

if(isFailure(result)) {
    //handle failure
} else {
    // handle success
}

The problem this solves

The way languages with shitty typesystems handle errors as values is by using types like Optional or Result. A lot of current error as value solutions in the Typescript world just copy this, this library aims to fix that and simple solution that leverages Typescripts unions to let you handle errors in a better way.

The main problem with Optional or Result types are that they can't be narrowed back down to their original types in a backwards compatible way.

Lets say we have a function that returns a Result type, and some code that handles it

function doSomething(x: number): Result<number, MyError> { ... }

const answer = doSomething(42);
if (answer.isError()) {
  // hande the error
}
// do something with the answer

Some time later the doSomething function gets updated so it can't error anymore. This should be a good thing but because we used a Result it breaks existing consumers of the function.

function doSomething(x: number): number { ... }

const answer = doSomething(42);
if (answer.isError()) { // Now crashes. isError doesn't exist on type number

Instead of using a wrapper class we can use unions and this library instead, which will allow narrowing without breaking existing consumers

before:

function doSomething(x: number): number | Failure { ... }

const answer = doSomething(42);
if (isFailure(answer)) {
  // hande the error
}
// do something with the answer

After removing the ability to error

function doSomething(x: number): number

const answer = doSomething(42);
if (isFailure(answer)) {
  // will never get run now, but won't crash/type error
}
// do something with the answer