# custom-errors-factory

> Construct custom error type

Latest version **1.0.0** (published 2018-02-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install custom-errors-factory
pnpm add custom-errors-factory
yarn add custom-errors-factory
bun add custom-errors-factory
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-02-25 |
| First published | 2017-12-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 17.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | mujichOk |
| Maintainers | mujichok |
| Keywords | error, custom, factory, inheritance |

## Links

- npm: https://www.npmjs.com/package/custom-errors-factory
- Repository: https://github.com/mujichOk/custom-errors-factory
- Homepage: https://github.com/mujichOk/custom-errors-factory#readme
- Issues: https://github.com/mujichOk/custom-errors-factory/issues
- npm.io page: https://npm.io/package/custom-errors-factory

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2018-02-25
- 0.1.1 — 2017-12-10
- 0.1.0 — 2017-12-10
- 0.0.1 — 2017-12-07

## README

# custom-errors-factory [![Build Status](https://travis-ci.org/mujichOk/custom-errors-factory.svg?branch=master)](https://travis-ci.org/mujichOk/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

```javascript
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! :)

```javascript
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:

```json
// config/errors.json
{
    "NotFound": {
        "message": "Not Found",
        "code": 404
    },
    "EntityNotFound": {
        "message": "[${entityType}] Entity Not Found"
    }
}
```

```javascript
// source/errors.js
const errorsFactory = require('custom-errors-factory');
const errorsConfiguration = require('config/errors.json');

module.exports = errorsFactory.createFromConfig(errorsConfiguration);
``` 

```javascript
// source/app.js
const {EntityNotFound} = require('./errors');

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

---
_Source: https://npm.io/package/custom-errors-factory · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
