custom-errors

custom error classes optimised for error logging

npm install custom-errors
11 downloads in the last day
24 downloads in the last week
175 downloads in the last month

Custom Error Classes

Build Status

  • 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

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 ValidationError(msg);

console.log('ValError', ValError);

STDOUT output:

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

Using with Express.js

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
    app.use(logErrors.production);
});

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
npm loves you