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

brilliant-errors

v0.7.3

Published

A set of configurators to help your Apps and Libraries build brilliant error classes

Downloads

16,385

Readme

Brilliant Errors

Note: the version 6.x of this library had moved to newer dependencies wand the callsites library only support the ESM module system these days but because we were packaging up in both ESM and CJS it was causing errors. I had intended to keep CJS around for a while longer but in most cases you really should be preferring ESM anyway. In version 7.x and going forward we are only publishing version ESM. If you still need CJS you can use the latest 0.5.x release.

The base errors you get from Javascript leave a lot to be desired and this library attempts to provide a consistent way to provide solid meta information on errors raised in Typescript/Javascript projects.

This library provides two configurators for errors:

  1. createError - the default error type provides an Error with strongly typed classification but does not require an HTTP status code for errors (you are allowed to add the HTTP status code)

    type ErrorType = "not-allowed" | "unexpected" | "missing-info";
    const MyError = createError<ErrorType>(e => e.name("MyError").origin("my-app"));
    
    // instantiation
    const error = new MyError("I've fallen and I can't get up", "unexpected/old-age");
       
    // static initializers for edge cases
    const error = MyError.withUnderlying(err, "No really, I can't get up", "unexpected/fer-fucks-sake");
    const error = MyError.withHttpCode(500, "Not worth repeating", "unexpected/web-fall");
    
    // options hash (means you can get to everything if you need it)
    const error = new MyError("Just stop it", "not-allowed/shit-happens", { underlying: err, httpStatus: 500 });
  2. createHttpError - an HTTP error produces errors which are guarenteed to have an HTTP status code as part of their payload but they also follow the Type/Subtype conventions in the base error.

    const HttpError = createHttpError<ErrorType, SubType>(e => 
        e.name("HttpError")
         .origin("my-network-app")
         .defaultType("missing-info")
    );
    
    // instantiotion
    const error = new HttpError(403, "couldn't find member's id", "no-membership-id");
    

All errors have the following attributes:

  • kind - a string literal type for the error family the error originates from
  • name - a string literal type for the error name
  • origin - a string literal type for the application/service which originated this error
  • classification - strongly typed Type/Subtype system
  • httpStatus - a numeric HTTP status code that can (and in some cases must) be set
  • underlying - all errors can store an underlying error and the wrapper error requires it

They also have brilliant .toJSON() and message outputs which shine above the plain old vanilla JS error.

For a repo which wants to use these configurators, you will create a file in your repo for the new error and then configure it something like this:

src/error/MyError.ts:

export default MyError = createLibraryError('MyError', { ... });

All options in the configurators are strongly typed which includes full documentatory information so rather than try to repeat that here we emplore you to use the built-in Typescript documentation within your code editor of choice.