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

howhap-list

v2.0.3

Published

An easy to maipulate list of howhap errors

Downloads

48

Readme

howhap list

npm install --save howhap-list

Stores a list of howhap errors for easy manipulation and display.

Usage

let HowhapList = require('howhap-list');

let list = new HowhapList({
	default: {
		message: 'An unknown error occurred',
		status: 500
	},
	email: {
		message: '{{email}} is an invalid email address',
		status: 400,
		params: {
			email: 'foo'
		}
	}
});

Constructor

let list = new HowhapList(errors, options);

Both errors and options are optional. Errors can be a plain vanilla javascript object that represents the list of keyed errors.

Methods

HowhapList.add(messageAndStatus, params, key)

Adds a new error to the list. The key defaults to 'default'. Below are some examples...

messageStatus can be an object
list.add({
	message: '{{email}} is an invalid email address',
	status: 400
}, {email: 'foo'}, 'email');

// RESULT:
/*
{
	email: {
		message: '{{email}} is an invalid email address',
		status: 400,
		params: {
			email: 'foo'
		}
	}
}
*/
messageStatus can be a string if the availableErrors option is supplied
list.add('AUTH.INVALID_EMAIL', {params: email: 'foo'}, 'email');

// RESULT:
/*
{
	email: {
		message: '{{email}} is an invalid email address',
		status: 400,
		params: {
			email: 'foo'
		}
	}
}
*/
neither params nor key are required
list.add('AUTH.INVALID_EMAIL');

// RESULT:
/*
{
	email: {
		message: '{{email}} is an invalid email address',
		status: 400
	}
}
*/
params can be supplied with out a key. The key will default to 'default'
list.add('AUTH.INVALID_EMAIL', {email: 'foo'});

// RESULT:
/*
{
	default: {
		message: '{{email}} is an invalid email address',
		status: 400,
		params: {
			email: 'foo'
		}
	}
}
*/
key can be supplied with out a params. The params will default to an empty object
list.add('AUTH.INVALID_EMAIL', 'email');

// RESULT:
/*
{
	email: {
		message: '{{email}} is an invalid email address',
		status: 400,
        params:{}
	}
}
*/

HowhapList.remove(key)

Removes an error by its key.

list.remove('default');

HowhapList.toJSON()

Returns a JSON representation of the howhap list with error objects rendered to strings.

let plainObject = list.toJSON();

HowhapList.toObject()

Returns a JSON representation of the howhap list with error objects preserved.

let plainObject = list.toObject();

HowhapList.display(key)

Displays a specific error based on its (optional) key

let list = new HowhapList({
	default: {
		message: 'An unknown error occurred',
		status: 500
	},
	email: {
		message: '{{email}} is an invalid email address',
		status: 400,
		params: {
			email: 'foo'
		}
	}
});

list.display(); // 'An unknown error occurred
list.display('default'); // 'An unknown error occurred
list.display('email'); // foo is an invalid email address

Options

options.availableErrors

availableErrors can hold an objet of errors that can be easily added to the list. For example:

// Errors are defined once in the options argument
let list = new HowhapList(null, {
	availableErrors: {
    	API: {
        	UNKNOWN: {
            	message: 'An unknown error occurred.',
                status: 500
            },
        	NOT_FOUND: {
            	message: 'The model with id {{id}} was not found.',
                status: 404
            },
            PERMISSION_DENIED: {
            	message: 'You don\'t have permission to {{action}} the resource {{resource}}',
                status: 401
            }
        },
        EMAIL: {
        	UNREACHABLE: {
            	message: 'The email address {{email}} is unreachable.',
                status: 400
            }
        }
    }
});

// Errors can be easily added via a string

list.add('API.UNKNOWN');
list.add('API.NOT_FOUND', {id: 7});
list.add('EMAIL.UNREACHABLE', {email: '[email protected]'});

options.logger

logger is an object that will be called each time an error is added. Works natively with bunyan but you can also add your own logger. It should adhere to the following interface (for example):

let logger = {
	debug: function(message, status, params) {
    	console.log('DEBUG:', message, status, params);
    },
    warn: function(message, status, params) {
    	console.log('WARN:', message, status, params);
    },
    error: function(message, status, params) {
    	console.log('ERROR:', message, status, params);
    }
};

Here's an example of how to configure your logger.

let bunyan = require('bunyan');
let HowhapList = require('howhap-list');

let list = new HowhapList(null, {
	logger: bunyan.createLogger({name: "myapp"})
});