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

@vamship/error-types

v2.0.1

Published

Library that defines custom error types

Downloads

87

Readme

@vamship/error-types

Exports custom error types that describe specific error conditions

Exports a set of classes that can be used to identify different types of errors. This is useful for post error processing, allowing for different behaviors for different error types.

Each error comes with a default error message, but also supports custom error messages. All error messages are prefixed with the argument name to help make predictable decisions based on error messages.

API Documentation

API documentation can be found here.

Motivation

Javascript is not a strongly typed language, but there are certain scenarios where having well defined types can be valuable. One of these scenarios pertain to error handling.

Let us consider the following promise chain:

const Promise = require('bluebird').Promise;
...

function fetchData(recordInfo) {
    Promise.try(() => {
        // Query the database for data. This call could succeed, or could throw
        // either:
        // (1) An error for failed schema validation
        // (2) An error if the record was not found
        queryDatabase(recordInfo)
    }).catch((err) => {
        //Check the error message and perform remedial actions.
        if(err.message === 'Schema validation failed') {
            //Deal with schema validation errors
        } else if(err.message === 'Record not found') {
            //Deal with record not found errors
        } else {
            //Handle all other errors.
        }
    });

In the above example, the exception handling code is clunky and brittle, depending on exact error message strings to make handling decisions. This can be cleaned up significantlly by using well defined error types like this:

const Promise = require('bluebird').Promise;
const { SchemaError } = require('@vamship/test-lib').args;
const { NotFoundError } = require('@vamship/test-lib').http;
...

function fetchData(recordInfo) {
    Promise.try(() => {
        // Query the database for data. This call could succeed, or could throw
        // either:
        // (1) SchemaError: If schema validation fails
        // (2) NotFoundError: If the record was not found
        queryDatabase(recordInfo)
    }).catch(SchemaError, (err) => {
        //Deal with schema validation errors
    }).catch(NotFoundError, (err) => {
        //Deal with record not found errors
    }).catch((err) => {
        //Handle all other errors.
    });

Strongly typed errors are also useful if the errors are processed outside the Javascript runtime, for example when an AWS API gateway attempts to handle an error thrown by a lambda function.

Installation

This library can be installed via npm using:

npm install @vamship/error-types

Usage

The types exposed by this library are available under two namespaces:

  • args: Error types for argument validation errors
  • http: Error types for http errors

Examples

HTTP Errors

const errorTypes = require('@vamship/error-types');
const {BadRequestError, NotFoundError, UnauthorizedError} = errorTypes.http;

// Throws an error with message '[BadRequestError] Incorrect or malformed request'
throw new BadRequestError()

// Throws an error with message '[NotFoundError] Resource not found'
throw new NotFoundError()

// Throws an error with message '[UnauthorizedError] Authorization failed'
throw new UnauthorizedError()

Argument Errors

const errorTypes = require('@vamship/error-types');
const {ArgError, SchemaError} = errorTypes.args;

// Throws an error with message '[ArgError] Arugment validation failed'
throw new ArgError()

// Throws an error with message '[SchemaError] Schema validation failed'
throw new SchemaError()