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 🙏

© 2024 – Pkg Stats / Ryan Hefner

failerr

v0.1.1

Published

Tools for type-safe handling of expected failure conditions through standard control flow

Downloads

87

Readme

failerr

Tools for type-safe handling of expected failure conditions through standard control flow.

Example

import { Fail, mkFail, isFail } from 'failerr';

const division = (num: number, div: number) => {
  if (div === 0) {
    return mkFail('division by zero', { code: 42 });
  }
  return num / div;
};

const res = division(7, 0);

if (isFail(res)) {
  res.message;    // The TypeScript compiler infers type "string"
  res.data.code;  // The TypeScript compiler infers type "number"
} else {
  res.message;    // The TypeScript compiler reports an error
}

Rationale

The use of Error-based exceptions as a strategy for dealing with failure conditions in the form of throw statements and try/catch blocks has a few issues:

  1. It's very slow
  2. It completely bypasses TypeScript's compile-time type checking
  3. It is a source of non-determinism and brittleness
  4. It conflates expected and unexpected failure conditions

Modern languages, such as Rust, have addressed these concerns through their native support of monadic types such as Option and Result. However, it is the author's opinion that userland JavaScript / TypeScript implementations of these structures are not a good solution due to their implications on code structure, performance and inspectability.

A better solution for handling expected failure conditions is to represent them as first-class values independent of the Error class and provide the necessary helper functions to instantiate them and and tell them apart from other values, implementing error handling through standard control flow.

An excellent piece of writing on this topic and a primary source of inspiration for this library can be found in Austral's approach to error handling.

API

The Fail interface represents expected failure conditions that a program should be able to handle while guaranteeing consistent internal state.

Fail objects are created using the mkFail() functions. The isFail() function helps with discriminating whether a value is a Fail object or not.

mkFail()

mkFail<D = {}>(message: string, data: D): Fail<D>

The mkFail() function can be used to create Fail objects whose .message and .data properties will be set to the provided arguments.

Simple Fail with empty data:

const fail: Fail = mkFail('some message');
fail.message;     // => 'some message'
fail.data;        // => empty, frozen object

Fail with specific data shape:

const fail: Fail<{ code: number }> = mkFail('some message', { code: 42 });
fail.message;     // => 'some message'
fail.data.code;   // => 42

isFail()

isFail(val: any): val is Fail<any>

The isFail() is a user-defined type guard that helps with identifying whether a value - normally the return value of a function - is a Fail object or not:

const value = (72 as Fail | number);

if (isFail(value)) {
  value.message;  // => TypeScript infers type "string"
}

When a function returns as the union of Fail and non-Fail

Extending the Fail interface

The Fail interface can be easily extended using types or interfaces:

type FailWithCode = Fail<{ code: number }>;

const fooBar = (): number | FailWithCode => {
  return Math.random() > 0.5 ? 42 : mkFail({ code: 42 });
};

Why an interface instead of a class?

Performance, mostly. Modeling Fail as an interface while instantiating implementations using simple objects seems to deliver between 1.5x and 1.75x greater throughput in high-performance applications (such as parsers) relative to using a class-based approach, though the difference decreases as code gets JIT-compiled over many iterations.

License

MIT