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

def-error

v0.9.2

Published

Define errors classes accessible across node processes, on the fly. Catchable with bluebird.

Downloads

11

Readme

def-error

Define errors classes accessible across node processes, on the fly. Catchable with bluebird.

Why ?

Creating a custom in Javascript is a bit of an hassle even though throwing custom prototypes errors is really useful specially with Promises (even more with bluebird Promises). This package allows to create Errors on the fly and intercepting them easily throughout a node process.

Defining and intercepting errors the classic ES2015 way

defining

class CustomError extends Error {
    constructor(){
        super();
    }
}

Then if you want to use the same error you need to export it and import it. So you will ceate a file let say errors.js and export all your errors this way :

// errors.js
class CustomError extends Error {
    constructor(){
        super();
    }
}

exports.CustomError = CustomError;

If you wanted to add more properties to your error, it would be even more of an hassle.

intercepting (with bluebird Promise)

// index.js
const CustomError = require('./errors').CustomError;
global.Promise = require('bluebird');

Promise.reject(new CustomError())
.catch(CustomError, error => {
    // catching my custom error
})

And then, you want to define a new error which can be use anywhere, well, you will have to go back in your errors file and define your error etc.

Defining and intercepting errors with def-error package

Your main script :

// index.js
const defError = require('def-error');

Promise.reject(new (defError('CustomError'))())
.catch(defError('CustomError'), error => {
    // catching my custom error
})

Done.

Moreover the error created here with defError will be available in another module using the defError package.

API

Define an error

const defError = require('def-error');

const MyCustomError = defError('MyCustomError');

throw new MyCustomError();
Adding custom message
throw new MyCustomError('My custom message');

Adding custom properties to error

You can add custom properties to the Error object by two ways.

Adding properties when defining

const MyCustomError = defError('MyCustomError', {
    foo: 'bar'
});

When you add properties when defining, the properties will be in every instance of the Error created.

let err = new MyCustomError();
console.log(err.foo) // bar

it will be overwritten if you do it again in the second way, which is when calling the constructor :

Adding properting when calling constructor

let err = new MyCustomError({
    foo: 'bar2'
});
console.log(err.foo) // bar2

Or if you want to define a message followed by props

let err = new MyCustomError('my custom error message', {
    foo: 'bar2'
});
console.log(err.foo) // bar2

Retrieve an error

Retrieving an error is pretty easy, it's the same way as defining an error. If the Error already exist - indexed by name - it will be returned, else it will be created.

const defError = require('def-error');

const MyCustomError = defError('MyCustomError');

Error store

All errors are stored in an Object, if you call defError() it will create the Error in the store if the error does not exist, otherwise it will return a the Error class created. Sometimes you might want to create an Error with the same name, but with a different signature. In that case, you can create your Error in a new store. You can do this by two means :

Use case