Skip to content

Repository files navigation

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

varmain=require('./../lib/main');varValidationError=main.general.ValidationError;varmsg="terrible input",ValError=newValidationError(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

varBadRequestError=main.request.BadRequest;varmsg="just an awful request",ReqError=newBadRequestError(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

varBadRequestError=require('customErrors').request.BadRequest;varlogErrors=require('log-errors');// define some routesapp.get('/some/route',function(req,res,next){if('error thrown'){next(newBadRequestError('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 loggerapp.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 setapp.use(logErrors.production);});

Reference

log levels

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

About

Custom Javascript Error Classes optimised for error logging

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages