Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including node-errors-helpers with all npm packages installed. Try it out:

var nodeErrorsHelpers = require("node-errors-helpers")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

node-errors-helpers v0.2.0

Some helpers for better error handling in Node.js

⚠ 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

ParamTypeDescriptionOptional
nameStringName the for class to generate.no
messageStringThe message calss instance will be thrown with.no
baseTypeStringClass 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)

ParamTypeDescriptionOptional
listObjectObject that define errors.no
extendAnyErrorTypeClass that all generated errors will extend.yes

helpers.getFullName(error)

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

Returns: String

ParamTypeDescriptionOptional
errorCustomErrorCustomErrorno

helpers.getObject(error)

Retruns iterable error data object.

For better errors representation in JSON format.

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

ParamTypeDescriptionOptional
errorCustomErrorCustomErrorno

helpers.getFullStack(error)

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

Returns: Array\<String>

ParamTypeDescriptionOptional
errorCustomErrorCustomErrorno

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

ParamTypeDescriptionOptional
errorCustomErrorWhere to look for class.no
ErrorClassAnotherCustomErrorWhat class to look for.no

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free