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

@b2b-alive/ts-error-generator

v1.1.0

Published

[![Build Status](https://travis-ci.org/b2b-alive-ltd/ts-error-generator.svg?branch=master)](https://travis-ci.org/b2b-alive-ltd/ts-error-generator) [![Coverage Status](https://coveralls.io/repos/github/b2b-alive-ltd/ts-error-generator/badge.svg?branch=ma

Downloads

2

Readme

Build Status Coverage Status

NPM

ts-error-generator

Custom errors and exceptions with TypeScript interface support in Node.js

This project is inspired by node-custom-error https://github.com/jproulx/node-custom-error/

Install

$ npm install @b2b-alive/ts-error-generator --save

Usage

import defineError, { ICustomError } from '@b2b-alive/ts-error-generator';

interface APIError extends ICustomError{
    required: string
}

interface HTTPError extends ICustomError{
    code: number,
    status: string
}

const ValidationError = defineError<APIError>('ValidationError', { 'required' : 'Missing parameter x' }, TypeError);
const HTTPError = defineError<HTTPError>('HTTPError', { 'code' : 500, 'status' : 'Server Error' });

throw new ValidationError('Required');

The generator function supports the following parameters:

  • name {String} (required) - A custom name for this error type, which is printed when logging and in stack traces

  • data {Object} (optional) - Additional properties to attach to the error, in key=value pairs or as object descriptors

  • Constructor {Function} (optional) - A function to inherit from. Allows for additional methods and properties to be defined on your custom errors

The errors created by the generated functions are identical to built-in Error objects, with additional features such as:

Custom properties can be attached and accessed at run time:

const error = new HTTPError('Uh oh');
console.log(error.code, error.status); // prints 500 Server Error

Formatting

Similar to console.log, the custom error message will be formatted from all available string and number arguments, using util.format:

const error = new ValidationError('%s: %s', 'Missing Field', 'name');
console.log(error); // prints ValidationError: Missing Field: name

Wrapped errors

Other error objects can be passed in as arguments, which augment the original error stack trace with their own stack traces:

const error = new ValidationError('Missing field');
const serverError = new HTTPError('Something went wrong', error);
console.log(serverError.stack);

outputs:

HTTPError: Something went wrong
    at Object.<anonymous> (/Projects/ts-error-generator/dist/test.js:10:21)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
ValidationError: Missing field
    at Object.<anonymous> (/Projects/ts-error-generator/dist/test.js:9:15)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)

Multiple error objects are allowed to be passed, and are processed in order.

JSON support

Errors can also be serialized into JSON format by using error#toJSON();. This will enumerate all of the hidden and custom properties, and also format the stack trace into an array of individual lines:

console.log(error.toJSON()); // or console.log(JSON.stringify(error));

outputs

{ stack:
   [ 'ValidationError: Required',
     '    at Object.<anonymous> (/Projects/ts-error-generator/dist/test.js:9:15)',
     '    at Module._compile (internal/modules/cjs/loader.js:688:30)',
     '    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)',
     '    at Module.load (internal/modules/cjs/loader.js:598:32)',
     '    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)',
     '    at Function.Module._load (internal/modules/cjs/loader.js:529:3)',
     '    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)',
     '    at startup (internal/bootstrap/node.js:285:19)',
     '    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)' ],
  message: 'Required',
  required: 'Missing parameter x' }

Custom Constructor

Finally, a custom function can be passed in as a 3rd argument. This will allow you to modify the custom error prototype without having to modify the original native Error prototype:

interface IAPIError extends ICustomError{
    code: number,
    status: string
    env: string
}

const APIError = defineError<IAPIError>('HTTPError', {env: process.env.NODE_ENV, route: '/'}, function (message: string, code: number) {
    const http = require('http');
    // Set custom properties when thrown based on additional arguments
    this.code = code;
    this.status = http.STATUS_CODES[code];
    // We can override the default message logic if desired:
    this.message = `${message} on ${this.route}`
});

const error = new APIError('You do not have permission', 403);
console.log(error.message, [error.code, error.status, error.env]);
// result: HTTPError: HTTPError: You do not have permission on / [ 403, "Forbidden" , "development"]

Notes

Care is taken to preserve the built-in error handling behavior as much as possible, supporting:

  • custom instanceOf Error

  • Error.prototype.isPrototypeOf(custom)

  • util.isError(custom)

  • custom = generator('message')

  • custom = new generator('message');

In other words, you shouldn't have to worry about these errors affecting your syntax or existing code. Simply drop in place for any existing errors you're throwing and it should work just the same.