custom-errors-factory
About
Make you errors more handy. Construct them with custom properties and templated messages. Build you own tree of errors inheritance. Use all power of types and best practicies from laguages with strong errors handling experience.
Install
npm install custom-errors-factory
Usage
const errorsFactory = require('custom-errors-factory');
const EntityNotFoundError = errorsFactory('EntityNotFound', {
message: '[${entityType}] entity not found',
entityType: 'Unknown'
});
const UserEntityNotFoundError = errorsFactory('UserEntityNotFound', {
entityType: 'User'
}, EntityNotFoundError);
const TestError = errorsFactory('Test');
const EntityNotFoundError = errorsFactory('EntityNotFound', {
message: '[${entityType}] entity not found',
entityType: 'Unknown'
});
const UserEntityNotFoundError = errorsFactory('UserEntityNotFound', {
entityType: 'User'
}, EntityNotFoundError);
const AnotherCustomError = errorsFactory('CustomError');
const CustomWithInnerError = errorsFactory('CustomWithInnerError')
.innerError(new Error('custom error'));
const customError = new UserEntityNotFoundError({someProperty: 'some additional property'});
console.log(customError instanceof Error);
console.log(customError instanceof EntityNotFoundError);
console.log(customError instanceof AnotherCustomError);
Configuration
You can create custom errors from predefined configuration with inheritance out of box! :)
const errorsFactory = require('custom-errors-factory');
const context = errorsFactory.createFromConfig({
BadRequest: {
base: 'HttpError',
message: 'Bad Request',
code: 400
},
EntityNotFound: {
message: '[${entityType}] Entity Not Found'
},
HttpError: {
base: 'Error',
message: 'HttpError',
code: 0
},
Error: {
message: 'Error'
}
});
Load custom errors configuration and use it like:
{
"NotFound": {
"message": "Not Found",
"code": 404
},
"EntityNotFound": {
"message": "[${entityType}] Entity Not Found"
}
}
const errorsFactory = require('custom-errors-factory');
const errorsConfiguration = require('config/errors.json');
module.exports = errorsFactory.createFromConfig(errorsConfiguration);
const {EntityNotFound} = require('./errors');
const customError = new EntityNotFound({entityType: 'File', path: '/some-path-to-file'});