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

typescript-chained-error

v1.6.0

Published

Chained custom errors for Typescript and Javascript. ('Caused by' in error stack)

Downloads

6,430

Readme

Typescript chained error

npm build Code coverage License PRs Welcome

Highlights

  • Allows stacking errors with "Caused by: " keyword in stack
  • Preserves error class name in trace (uses ts-custom-error for that)
  • Automatically cleans up stack using clean-stack](https://github.com/sindresorhus/clean-stack)
  • Has a ChainedErrorFactory that can extend any already existing error with Caused by clause additional properties and
  • 100% test coverage. Used in healthcare-grade solutions

Installation

$ npm install --save typescript-chained-error

Usage

  1. Define custom error class with a constructor that has a cause as a second parameter
  2. Throw your custom error passing the causing error as a second parameter
  3. Error causes can be stacked (there may be many causes that are finally leading to the printed error)
import ChainedCustomError from "typescript-chained-error";

class CannotPerformCalculationsError extends ChainedCustomError {
    public constructor(msg?: string, cause?: Error) {
        super(msg, cause);
    }
}

// function that may throw
function buildArray(desiredLength: number) {
    try {
        return new Array(desiredLength);
    } catch (error) {
        throw new CannotPerformCalculationsError("Cannot build array", error);
    }
}

// call function with illegal parameter
buildArray(-5);

The output:

CannotPerformCalculationsError: Cannot build array
    at buildArray (/typescript-chained-error/src/test.ts:14:15)
    at Object.<anonymous> (/typescript-chained-error/src/test.ts:18:1)
    (...)
 Caused by: RangeError: Invalid array length
    at buildArray (/typescript-chained-error/src/test.ts:12:16)
    at Object.<anonymous> (/typescript-chained-error/src/test.ts:18:1)
    (...)

Options

interface Options {
    cleanStack: boolean; // default: true
}

// Pass options in constructor
public constructor(msg?: string, cause?: Error) {
    super(msg, cause, { cleanStack: false });
}

Using ChainedErrorFactory

Example with firebase-functions https error (which is the only error that is thrown at the call site).

import { ChainedErrorFactory } from "typescript-chained-error";
import * as functions from "firebase-functions";

throw ChainedErrorFactory.make(
    // primary error that will be extended (this error is preserved in the prototype chain):
    functions.https.HttpsError("resource-exhausted", "Message"),
    // Causing error:
    new TypeError("Cause error"),
    // (optional) Additional fields that will be assigned to the returned error object
    // e.g.: functions.https.HttpsError allow to add a details field to the error. That field will be reconstructed at the call site.
    /* optional: */ { details: { additional: "properties" } },
    // (optional) Options. Specified above.
);

Extends the brilliant ts-custom-error. Uses clean-stack by @sindresorhus for pretty output. | Made with ❤️ by Jędrzej Lewandowski.