# node-errors-helpers

> Some helpers for better error handling in Node.js

Latest version **1.0.0** (published 2021-06-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-errors-helpers
pnpm add node-errors-helpers
yarn add node-errors-helpers
bun add node-errors-helpers
```

## 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 | 2021-06-08 |
| First published | 2016-06-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Mark Orel |
| Maintainers | shmuga, dchertousov, travelport-ua |
| Keywords | error, helpers |

## Links

- npm: https://www.npmjs.com/package/node-errors-helpers
- Repository: https://github.com/Travelport-Ukraine/errors-helpers
- Homepage: https://github.com/Travelport-Ukraine/errors-helpers#readme
- Issues: https://github.com/Travelport-Ukraine/errors-helpers/issues
- npm.io page: https://npm.io/package/node-errors-helpers

## 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) — 2021-06-08
- 0.3.0 — 2020-11-20
- 0.2.1 — 2020-09-15
- 0.2.0 — 2019-09-25
- 0.1.3 — 2017-05-16
- 0.1.2 — 2016-10-27
- 0.1.1 — 2016-10-20
- 0.1.0 — 2016-10-19
- 0.0.4 — 2016-06-14
- 0.0.3 — 2016-06-13
- 0.0.2 — 2016-06-13
- 0.0.1 — 2016-06-13

## README

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

This library helps with error handling in Node.

It provides following API:
- Generators API
    - Custom error creation (with inheritance and chaining)
    - Creates lists of errors from simple key-value objects
- Helpers API
    - Getting iterable object from error instance
    - Getting full name for inherited error instance
    - Getting array of stacks for an error chain
    - Detecting if there is an error of particular error class in the chain

## 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',
  'Runtime error',
  Error // parent error class, optional
);

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

throw new SWError();
throw new StarWarsErrors.YOUR_FATHER({ location: 'Bespin' });
throw new StarWarsErrors.NO_LUKE({ far: 'away' }, causedByError /* some generated or catched error */);
```

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

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

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

Creates error class with the `name` provided, that will throw error with `message`. When `baseType` class provided, new error class will be extending base one, if not `Error` class is extended. Each error generated with this helper will have specific structure and [constructor](#customerror).

**Returns**: `CustomError`

| Param | Type | Description | Optional |
| --- | --- | --- | --- |
| name | `String` | Name the for class to generate. | no |
| message | `String` | The message calss instance will be thrown with. | no |
| baseType | `String` | Class to extend. If not provided `Error` class is extended. | yes |

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

Created error class will throw error instance with [`message`](#createclass) provided in class constructor.
Field `data` has `any` type. So you can pass there everything.

You can pass an 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)
Generates object of errors from object of definitions.
If no `extend` class provided error classes are extended from `Error`.

Object sample:
```javascript
{
  'SOME_ERROR_CODE': 'Message that will be shown.',
  'ANOTHER_ERROR': 'Another message %)'
}
```
**Returns**: `Object (key - code, value - Error)`

| Param | Type | Description | Optional |
| --- | --- | --- | --- |
| list | `Object` |  Object that define errors. | no |
| extend | `AnyErrorType` |  Class that all generated errors will extend. | yes |

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

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

**Returns**: `String`

| Param | Type | Description | Optional |
| --- | --- | --- | --- |
| error | `CustomError` | CustomError | no |


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

Retruns iterable error data object.

For better errors representation in JSON format.

**Returns**: `Object { name, stack, message, data, causedBy }`

| Param | Type | Description | Optional |
| --- | --- | --- | --- |
| error | `CustomError` | CustomError | no |


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

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

**Returns**: `Array\<String>`

| Param | Type | Description | Optional |
| --- | --- | --- | --- |
| error | `CustomError` | CustomError | no |

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

Looks for `ErrorClass` in `error`. Recursivly looks in `causedBy` fields.
Returns `true` if an instance of `ErrorClass` is found, `false` otherwise.


**Returns**: `Boolean`

| Param | Type | Description | Optional |
| --- | --- | --- | --- |
| error | `CustomError` | Where to look for class. | no |
| ErrorClass | `AnotherCustomError` | What class to look for. | no |

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