# immutable-error

> Generate errors for the Immutable App ecosystem

Latest version **0.1.5** (published 2018-11-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install immutable-error
pnpm add immutable-error
yarn add immutable-error
bun add immutable-error
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.5 |
| Published | 2018-11-18 |
| First published | 2017-09-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=7.6.0 |
| Dependencies | 2 |
| Unpacked size | 14.4 KB |
| Known vulnerabilities | 0 (+6 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ersun Warncke |
| Maintainers | ersun.warncke |

## Links

- npm: https://www.npmjs.com/package/immutable-error
- Repository: https://github.com/warncke/immutable-error
- Homepage: https://github.com/warncke/immutable-error#readme
- Issues: https://github.com/warncke/immutable-error/issues
- npm.io page: https://npm.io/package/immutable-error

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) 4.17.11
- [if-defined](https://npm.io/package/if-defined.md) 1.0.18

## Recent versions

- 0.1.5 (latest) — 2018-11-18
- 0.1.4 — 2018-01-15
- 0.1.3 — 2017-10-06
- 0.1.2 — 2017-10-04
- 0.1.1 — 2017-09-27
- 0.1.0 — 2017-09-27

## README

# immutable-error

Immutable Error provides harmonized error generator for modules in the
[Immutable App](https://www.npmjs.com/package/immutable-app) ecosystem.

## Creating a new error generator instance

    var immutableError = new ImmutableError({
        class: 'Foo',
        errorCodes: {
            100: 'foo error'
        },
        nameProperty: 'foo',
    })

ImmutableError must be instantiated with an object containing the configuration
options for the error generator.

The `class` property will be used as a prefix for all error messages.

The `codes` object defines a mapping of error codes to default error messages.
Error codes must start at a number grater than 100 with a maximum length of
three digits.

The `nameProperty` property will be used to get the name of the instance that
the error occured with if both are set.

## error

    var error = immutableError.error(this, 100, 'foo error', err, {foo: true})

The `error` method generates and returns a new Error.

The arguments to error are:

* instance - the instance (this) context that the error occurred in
* code - the error code
* message - a custom error message
* error - the original Error object if re-throwing a caught error
* data - any additional error data to include

All arguments are optional and an error will always be generated.

## assert

    immutableError.assert(foo === true, this, 100)

The `assert` method will evaluate its first argument and throw an error if it does
not evaluate as truthy.

The subsequent arguments to assert will be passed to error to generate the
Error.

Unlike `error` the `assert` method will throw the Error.

## throw

    immutableError.throw(this, 100)

The `throw` method calls `error` with the passed arguments and throws the
generated error.

## Generating an error without an instance

    var error = immutableError.error(null, 100)

If an error is being generated outside of the context of a specific class
instance then null should be passed as the first argument.

## Generating an error without a code

    var error = immutableError.error(this, null, 'foo')

If a code is not available for the error null or 0 should be used.

## Generating an error with a custom error message

    var error = immutableError.error(this, 100, 'custom error message')

A custom error can be passed as the third argument. If this argument is not a
string it will not be used.

## Generating an error from an existing error object

    try {
        ...
    } catch (err) {
        var error = immutableError.error(this, 100, null, err)
    }

When catching and rethrowing an error pass the original error as the fourth
argument and it will be added to the data of the error that is generated.

## Error codes

The error codes defined by modules must be three digits starting with 100.

The error code will be used to lookup the default message for the error.

For registered classes the error code will be appended to a two digit prefix
that identifies the class that generated the error.

If the class generating the error is not registered then the error code will
always be 10000.

For classes that are registered where an error code is not specified the
error code will be the class prefix followed by three zeros.

The three digit error code will always be stored in `error.data.internalCode`.

### Registered classes

| base code | class                                                            |
|-----------|------------------------------------------------------------------|
| 20000     | ImmutableAppComponent                                            |

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