custom-errors
v1.5.1
Published
custom error classes optimised for error logging
Downloads
38
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
- add more tests
- 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