custom error classes optimised for error logging

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
  • Easy logging integration with express.js using the log errors module
1. ValidationError
2. DatabaseError
1. BadRequest = Bad Request Error (400)
2. Unauthorized = Unauthorized Error (401)
3. Forbidden = Forbidden Error (403)
4. NotAcceptable Request Not Acceptable Error (406)
var main            = require('./../lib/main');
var ValidationError = main.general.ValidationError;
var msg      = "terrible input",
    ValError = new ValidationError(msg);
console.log('ValError', ValError);

STDOUT output:

ValError {
    name: 'Validation',
    logLevel: 'warning',
    doNotKill: undefined,
    resCode: undefined,
    message: 'not a valid date'
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: 'warning',
    resCode: 400,
    message: 'just an awful request'

Easy integration with the log errors module

var BadRequestError = require('customErrors').request.BadRequest;
var logErrors   = require('log-errors');
// define some routes
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 beneath all routes)
app.configure('development', function() {
    // wrap the logger if you need to do something
    // with the error before passing it to the logger
    app.use(function(err, req, res, next) {
        err.resCode || (err.resCode = 400);
        logErrors.development(err, req, res, next);
app.configure('production', function() {
    // defaults to sending 500 response if err.resCode is not set

Header status codes

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