<commit_before># ⚠ Errors Helpers [![Build Status](https://travis-ci.org/Travelport-Ukraine/errors-helpers.svg?branch=master)](https://travis-ci.org/Travelport-Ukraine/errors-helpers)

Current lib helps with error handling in Node.
It provides few helpers that makes life easier. If you want error handling and creation be easy use this lib.
 
## Installation

Run `npm install --save node-errors-helpers`

## Example

Simple usage of this lib.

```javascript
const e = require('node-errors-helpers');
const StarWarsErrors = e.generate({
    'NO_LUKE': 'No Luke Skywalker!',
    'NO_DARTH': 'No Darth Vader!',
    'YOUR_FATHER': 'I am you father, Luke!',
  }, 
  Error);

throw new StartWarsErrors.YOUR_FATHER({ /* additional data */ });
throw new StartWarsErrors.NO_LUKE({ far: 'away' }, CasedByError /* some error */);
```


## API

* [get(error)](#getError) ⇒ <code>Object</code>
* [generate(list, extend, [customFields, [getErr]])](#generateFromList) ⇒ <code>Object</code>
* [inner(error, ErrorClass)](#inner) ⇒ <code>Boolean</code>
* [lib](#FBPlatform+sendImage) ⇒ <code>Object</code>

<a name="getError"></a>
### get(error)

Generate object representation of the Error.

**Returns**: <code>{ code, message, stack, data }</code>

| Param | Type | Description |
| --- | --- | --- |
| error | <code>Error</code> | Any error created with [generateFromList](#generateFromList) |


<a name="generateFromList"></a>
### generate(list, extend, [customFields, [getErr]])]
Generate array of errors from object of definitions.
Object sample: 
```javascript
{
  'SOME_ERROR_CODE': 'Message that will be shown.',
  'ANOTHER_ERROR': 'Another message %)'
}
```
**Returns**: <code>Object (key - code, value - Error)</code>

| Param | Type | Description |
| --- | --- | --- |
| list | <code>Object</code> |  Object that define errors. |
| extend | <code>AnyErrorType</code> |  Class that will all generated errors extend. |
| customFields | <code>Array\<String\></code> | Additional fields that each error will have. Objects will contain fields as props of an object. Default `['data']` |
| getErr | <code>Function(AnyErrorType)</code> | Function that generate object from error. Default: [getError](#getError) |


<a name="inner"></a>
### inner(error, ErrorClass)

Check if error has any inner errors with ErrorClass.

**Returns**: <code>Boolean</code>

| Param | Type | Description |
| --- | --- | --- |
| error | <code>Error</code> | Any error created with [generateFromList](#generateFromList) |
| ErrorClass | <code>Error</code> | Any Error from Node or current lib. |

<a name="lib"></a>
### lib

This lib points to [node-common-errors](https://github.com/shutterstock/node-common-errors)<commit_msg>Updated README.<commit_after>   # ⚠ Errors Helpers [![Build Status](https://travis-ci.org/Travelport-Ukraine/errors-helpers.svg?branch=master)](https://travis-ci.org/Travelport-Ukraine/errors-helpers)

Current lib helps with error handling in Node.
It provides few helpers that makes life easier. If you want error handling and creation be easy use this lib.
 
## Installation

Run `npm install --save node-errors-helpers`

## Example

Simple usage of this lib.

```javascript
const { createErrorClass, createErrorsList } = require('errors-helpers');

const SWError = createErrorClass(
	'RuntimeError', 
    'Error during validation.', 
    Error
);

const StarWarsErrors = createErrorsList({
    'NO_LUKE': 'No Luke Skywalker!',
    'NO_DARTH': 'No Darth Vader!',
    'YOUR_FATHER': 'I am you father, Luke!',
  }, 
  SWError);

throw new StartWarsErrors.YOUR_FATHER({ /* additional data */ });
throw new StartWarsErrors.NO_LUKE({ far: 'away' }, causedByError /* some error */);
```

[More examples.](/examples/generateFromList.js)

## API
* [createErrorClass(name, message, baseType)](#createclass) ⇒ [<code>CustomError</code>](#customerror)
* [createErrorsList(list, extend)](#createlist) ⇒ <code>Object</code>
* helpers
  * .[getFullName(error)](#fullname) ⇒ <code>String</code>
  * .[getObject(error)](#object) ⇒ <code>Object</code>
  * .[getFullStack(error)](#fullstack) ⇒ <code>Array\<String></code>
  * .[hasErrorClass(error, ErrorClass)](#haserrorclass) ⇒ <code>Boolean</code>

<a name="createclass"></a>
### createErrorClass(name, message, baseType))

Creates class with `name` that will throw error with `message` that extends `baseType` class. Each error generated with this helper will have specific structure and [constructor](#customerror). 

**Returns**: <code>CustomError</code>

| Param | Type | Description |
| --- | --- | --- |
| name | <code>String</code> | Name for class to generate. |
| message | <code>String</code> | Message for class to throw. |
| baseType | <code>String</code> | Class to extend. |

<a name="customerror"></a>
#### CustomError

Any custom error has such constructor: 
```javascript
const err = new CustomError(data = null, causedBy = null);
	// err.data
    // err.name
    // err.causedBy
```

By default such error class will throw error with [`message`](#createclass).
Field `data` has `any` type. So you can pass there everything. 

You can pass some error as second param and it will be saved as cause of current error. See [examples](/examples/generateFromList.js) for more information.

<a name="createlist"></a>
### createErrorsList(list, extend)
Generate object of errors from object of definitions.
Object sample: 
```javascript
{
  'SOME_ERROR_CODE': 'Message that will be shown.',
  'ANOTHER_ERROR': 'Another message %)'
}
```
**Returns**: <code>Object (key - code, value - Error)</code>

| Param | Type | Description |
| --- | --- | --- |
| list | <code>Object</code> |  Object that define errors. |
| extend | <code>AnyErrorType</code> |  Class that all generated errors will extend. |

<a name="fullname"></a>
### helpers.getFullName(error)

Returns full name of error. Concats all parent classes names with `.`

**Returns**: <code>String</code>

| Param | Type | Description |
| --- | --- | --- |
| error | <code>CustomError</code> | CustomError |


<a name="object"></a>
### helpers.getObject(error)

Retruns object.

For better errors representation in JSON format. 

**Returns**: <code>Object { name, stack, message, data, causedBy }</code>

| Param | Type | Description |
| --- | --- | --- |
| error | <code>CustomError</code> | CustomError |


<a name="object"></a>
### helpers.getFullStack(error)

Recursivly gets stacks from `causedBy` errors and return `Array` of them.

**Returns**: <code>Array\<String></code>

| Param | Type | Description |
| --- | --- | --- |
| error | <code>CustomError</code> | CustomError |

<a name="haserrorclass"></a>
### helpers.hasErrorClass(error, ErrorClass)

Looks for `ErrorClass` in `error`. Recursivly looks in `causedBy` fields.


**Returns**: <code>Boolean</code>

| Param | Type | Description |
| --- | --- | --- |
| error | <code>CustomError</code> | Where to look for class. |
| ErrorClass | <code>AnotherCustomError</code> | What class to look for. |

{'fraction_non_alphanumeric': 0.17026171987345412, 'fraction_numerical': 0.0, 'mean_word_length': 4.3789636504253675, 'pattern_counts': {'":': 0, '<': 100, '<?xml version=': 0, '>': 100, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6970055', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2189, 'n_words': 632}