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

custom-errors

v1.5.1

Published

custom error classes optimised for error logging

Downloads

115

Readme

Custom Error Classes

  • All classes inherit from the abstract class (lib/abstract-error.js) inspired by dustin senos's post.
  • The abstract error class inherits from the in built error object.
  • All error classes exported in lib/main.js -> index.js

App Error Classes (module.exports.general)

1. ValidationError
2. DatabaseError

Express Error Classes (module.exports.request)

1. BadRequest = Bad Request Error (400)
2. Unauthorized = Unauthorized Error (401)
3. Forbidden = Forbidden Error (403)
4. NotAcceptable Request Not Acceptable Error (406)

Sample Usage

1. General Error Classes

var main = require('./../lib/main');
var ValidationError = main.general.ValidationError;

var msg = "terrible input",
	ValError = new BadRequest(msg);

console.log('ValError', ValError);

STDOUT output:

ValError {
	name: 'Validation',
	logLevel: 'warn',
	resCode: undefined,
	message: 'not a valid date'
}

2. Request Error Classes

var BadRequestError = main.request.BadRequest;
var msg = "just an awful request",
	ReqError = new BadRequestError(msg);

console.log('ReqError', ReqError);

STDOUT output:

ReqError {
	name: 'BadRequest',
	logLevel: 'warn',
	resCode: 400,
	message: 'just an awful request'
}

In-App Usage eg in Express

// route definitions
var customErrors = require('customErrors');
var BadRequestError = customErrors.request.BadRequest;

app.get('/some/route', function(req, res, next) {
	if ('error thrown') {
		next(new BadRequestError('reason for the bad request being thrown'));
	}
});

// catchall error middleware (put at very end underneath all routes)
var	util = require('util');
var Log = require('log');
var	log = new Log();
var customErrors = require('customErrors');
var BadRequestError = customErrors.request.BadRequest;

app.use(function(err, req, res, next) {

	if (err instanceof BadRequestError && app.get('env') === 'production') {
		// use visionmedia's logger to write to main app logfile
		var logLevel = err.logLevel || 'debug';
		var logger = log[logLevel];
		var seriousErrors = ['error', 'critical', 'alert', 'emergency'];

		// log the error summary with visionmedia's logger
		logger(err.name + ': ' + err.message);

		// if it's a serious error then dump some more info for debugging purposes
		if (seriousErrors.indexOf(logLevel) !== -1) {
			console.log(err.stack);
		}

	} else {
		// if in dev then deeply inspect every error
		console.log(err.name || 'express error', util.inspect(err, true, 4, true));
	}

	res.send(err.resCode || 500, {error: err.name || "there was an error"});
});

TODO

  1. add more tests
  2. add visionmedia's log as dependency -> walk thru list of methods and throw error logLevel property is not a method

eg this shd throw:

errFactory('Database', 'err')

eg this is correct:

errFactory('Database', 'error')

Reference

Header status codes

log levels

Visionmedia Logging Module
0 EMERGENCY system is unusable
1 ALERT action must be taken immediately
2 CRITICAL the system is in critical condition
3 ERROR error condition
4 WARNING warning condition
5 NOTICE a normal but significant condition
6 INFO a purely informational message
7 DEBUG messages to debug an application