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

node-errors-helpers

v1.0.0

Published

Some helpers for better error handling in Node.js

Downloads

4,988

Readme

⚠ Errors Helpers Build Status

This library helps with error handling in Node.

It provides following API:

  • Generators API
    • Custom error creation (with inheritance and chaining)
    • Creates lists of errors from simple key-value objects
  • Helpers API
    • Getting iterable object from error instance
    • Getting full name for inherited error instance
    • Getting array of stacks for an error chain
    • Detecting if there is an error of particular error class in the chain

Installation

Run npm install --save node-errors-helpers

Example

Simple usage of this lib.

const { createErrorClass, createErrorsList } = require('errors-helpers');

const SWError = createErrorClass(
  'RuntimeError',
  'Runtime error',
  Error // parent error class, optional
);

const StarWarsErrors = createErrorsList({
  'NO_LUKE': 'No Luke Skywalker!',
  'NO_DARTH': 'No Darth Vader!',
  'YOUR_FATHER': 'I am you father, Luke!',
}, SWError);

throw new SWError();
throw new StarWarsErrors.YOUR_FATHER({ location: 'Bespin' });
throw new StarWarsErrors.NO_LUKE({ far: 'away' }, causedByError /* some generated or catched error */);

More examples.

API

createErrorClass(name, message, baseType))

Creates error class with the name provided, that will throw error with message. When baseType class provided, new error class will be extending base one, if not Error class is extended. Each error generated with this helper will have specific structure and constructor.

Returns: CustomError

| Param | Type | Description | Optional | | --- | --- | --- | --- | | name | String | Name the for class to generate. | no | | message | String | The message calss instance will be thrown with. | no | | baseType | String | Class to extend. If not provided Error class is extended. | yes |

CustomError

Any custom error has such constructor:

  const err = new CustomError(data = null, causedBy = null);
  // err.data
  // err.name
  // err.causedBy

Created error class will throw error instance with message provided in class constructor. Field data has any type. So you can pass there everything.

You can pass an error as second param and it will be saved as cause of current error. See examples for more information.

createErrorsList(list, extend)

Generates object of errors from object of definitions. If no extend class provided error classes are extended from Error.

Object sample:

{
  'SOME_ERROR_CODE': 'Message that will be shown.',
  'ANOTHER_ERROR': 'Another message %)'
}

Returns: Object (key - code, value - Error)

| Param | Type | Description | Optional | | --- | --- | --- | --- | | list | Object | Object that define errors. | no | | extend | AnyErrorType | Class that all generated errors will extend. | yes |

helpers.getFullName(error)

Returns full name of error. Concats all parent classes names with ..

Returns: String

| Param | Type | Description | Optional | | --- | --- | --- | --- | | error | CustomError | CustomError | no |

helpers.getObject(error)

Retruns iterable error data object.

For better errors representation in JSON format.

Returns: Object { name, stack, message, data, causedBy }

| Param | Type | Description | Optional | | --- | --- | --- | --- | | error | CustomError | CustomError | no |

helpers.getFullStack(error)

Recursively gets stacks from causedBy errors and return Array of them.

Returns: Array\<String>

| Param | Type | Description | Optional | | --- | --- | --- | --- | | error | CustomError | CustomError | no |

helpers.hasErrorClass(error, ErrorClass)

Looks for ErrorClass in error. Recursivly looks in causedBy fields. Returns true if an instance of ErrorClass is found, false otherwise.

Returns: Boolean

| Param | Type | Description | Optional | | --- | --- | --- | --- | | error | CustomError | Where to look for class. | no | | ErrorClass | AnotherCustomError | What class to look for. | no |