Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

custom-errors-factory

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

custom-errors-factory

Construct custom error type

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

custom-errors-factory Build Status

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); // true
console.log(customError instanceof EntityNotFoundError); // true
console.log(customError instanceof AnotherCustomError); // false

Configuration

You can create custom errors from predefined configuration with inheritance out of box! :)

const errorsFactory = require('custom-errors-factory');

// from config object (note: you can use it for async load from remote server, for instance)
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:

// config/errors.json
{
    "NotFound": {
        "message": "Not Found",
        "code": 404
    },
    "EntityNotFound": {
        "message": "[${entityType}] Entity Not Found"
    }
}
// source/errors.js
const errorsFactory = require('custom-errors-factory');
const errorsConfiguration = require('config/errors.json');

module.exports = errorsFactory.createFromConfig(errorsConfiguration);
// source/app.js
const {EntityNotFound} = require('./errors');

const customError = new EntityNotFound({entityType: 'File', path: '/some-path-to-file'});

Keywords

FAQs

Package last updated on 25 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc