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

errorsasvaluests

v1.0.5

Published

Errors-As-Values Error Handling

Readme

Expected

Github Repo NPM Package

1. Making an Expected instance

API

static Expected.ok<T>(value: T);
static Expected.fail<E>(value: E);

T: The return type

E: The error type

Example

function myFunc(): Expected<void, Error> {
    try {
        // do work
        return Expected.ok(void);
    } catch (e) {
        return Expected.fail(e);
    }
}

2. Wrapping a function

API

static Expected.run<T, E>(fn: (...args: unknown[]) => T | Expected<T, E>,
            args?: any[],
            errorType?: new (...args: any[]) => E): Expected<T, E>

fn: The function to call

args?: What arguments to use, if any.

errorType?: The constructor for the error type. If errorType is provided, and the error thrown by the function doesn't match errorType, the error will be propogated outwards rather than returned as a value.

Expected<T, E>.onError(
    fn: (
        errorValue: E, 
        setReturnValue: (value: T) => void
    ) => void
): T | undefined

fn: A handler for any occuring errors

errorValue: The error value

setReturnValue: By default, onError() returns undefined and calls your handler if any error occures, otherwise returning the returned value. For your handler to replace the placeholder undefined, call setReturnValue() with the value you'd like onError to return.

Example

function myFunc(): void {
    throw Error("3");
}

const value = Expected.run(myFunc).onError((e, h) => h(Number(e.message)));  // 3

Tips

If the function returns an instance of Expected, that exact instance will be returned by run().

If the function passed to run() returns any other value, it will get wrapped into Expected.ok(value).

If the function passed to run() throws an error:

  • If no errorType constructor is specified, it will get wrapped into Expected.fail(error)
  • If an errorType constructor is specified:
    • If the error matches the constructor (runtime type validation), it will get wrapped into Expected.fail(error)
    • If the error doesn't match the constructor, it will get re-thrown.
  • The library provides a throwError(e) function that throws an error, that you can pass to onError(). However, you should prefer getValueUnsafe() over this to keep code shorter.

3. Leaving errors unhandled

API

static Expected.getValueUnsafe<T>(expected: Expected<T, any>): T

Returns the value stored within an Expected instance. If expected is an Expected.fail, then an error will be thrown with the error stored by expected (note: it does not have to be an Error instance).

Good for when you want your program to crash if a function fails, or if you need to maintain backwards-compatibility with a caller.