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

errors-in

v1.2.1

Published

Removing try-catch boilerplate in a natural style with error handlers.

Readme

errorsIn

Removing try-catch boilerplate in a natural style with error handlers.

Quick Start

Installation

npm install errors-in

Common Usage

const errorsIn = require("errors-in");

const run = errorsIn(() => {
    throw new Error("Whoops!");
})
.willBeCaughtBy(err => {
    console.log("Bob caught the error first.");
    throw err;
}, anotherCatcher)
.and(async err => {
    console.log("Alice caught the error async.");
});

Other usages and sorry

Return Value

A value can be returned both at the function and the catcher. A Joi style validation result can be mocked in this way.

const run = errorsIn(number => {
    if (number > 0.5) {
        return { number };
    } else {
        const err = new Error("Nah");
        throw err;
    }
})
.willBeCaughtBy((err, number) => {
    return { number, error: err };
});

Catcher Arguments

The first argument of the catcher will obviously be the error. The rest are the arguments received in the function forwarded.

const catcher = (err, req, res) => {
    console.log(err);
    res.status(500)
            .send("Oops. Something is on fire.");
}

Although express.js provides centralized error handling, it still lacks flexibility due to its over-centralization. Which error handling method you would choose is totally up to you, yet express.js used in this case is only for demonstration.

const express = require("express");
const app = express();
... ... // Set up the app

app.post("/api/boopybot", errorsIn(async (req, res) => {

    // A TypeError will be thrown if body parser is missing
    if (req.body.userId === "honeybadger") {
        res.send("Beep boop, boop beep.");
    } else {
        res.send("Beep beep, beep boop.");
    }

}).willBeCaughtBy(catcher));

Not built specifically for express.js, errorsIn can be used in any case where the error handling mechanism is poorly structured.

Async and When Will It

The wrapped function will return a promise behaves the same as sync but in promises when it reaches any promise-returning middleware, which can be either an async function or an async catcher.

const run = errorsIn(func)
    .willBeCaughtBy(catcher1)
    // ↓ Returns a Promise responsible for the rest catchers
    .and(/*async*/ catcher2) 
    .and(catcher3);

// Successful Return at func:     Not Promise
// Successful Return at catcher1: Not Promise
// Successful Return at catcher2: A Promise
// Successful Return at catcher3: A Promise
const result = /*await*/ run(); 

Context

It's sometimes frustrating when the error is oversimplified. That's when you need some context. The context object can provides what you need.

const contextOf = errorsIn.contextOf;

const run = errorsIn(($var, id) => {
    $var.name = database.get(id, "name");

    if (hasSpecialCharacter($var.name)) 
        throw "I'm the Baaaaad error. Duh.";

}).withContext().willBeCaughtBy((err, id) => {

    const $var = contextOf(err);

    if ($var.name.contains("*")) 
        console.log("Asterisk is not allowed in names.");
});

When specifying with .withContext(), a context object, which is a blank object that can be shared between the function and catchers, will be passed as the first argument. There's also .withoutContext(), which does the opposite to .withContext(). By default, the context is not used, and you have to specify it.

You can acquire the context object in error catchers by using contextOf(err). The context object will be unavailable by calling this function after all catchers have done their job. However, you can still acquire it by setting variable outside or return it.

let context;

const catcher = err => {
    const $var = contextOf(err);
    context = $var; // DIRTY!
    // Or better
    return { context: $var, error: err };
}

It's not necessary to name the context object $var, but you can make it look more natural by doing this, as if you're dealing with variables.

Where is the documentation?

Since I just came up with it, the documentation is work in progress and soon will be online alongside with a git repository. Sorry for the inconvenience.