# error-provider

> Manages errors for third-party modules

Latest version **0.0.6** (published 2013-03-10) · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2013-03-10 |
| First published | 2013-01-20 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | * |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Gabriel Llamas |
| Maintainers | Gagle |
| Keywords | error, errno, provider, libuv |

## Links

- npm: https://www.npmjs.com/package/error-provider
- Repository: https://github.com/Gagle/Node-ErrorProvider
- npm.io page: https://npm.io/package/error-provider

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

- 0.0.6 (latest) — 2013-03-10
- 0.0.5 — 2013-03-05
- 0.0.4 — 2013-03-01
- 0.0.3 — 2013-02-27
- 0.0.2 — 2013-01-20

## README

error-provider
==============

_Node.js project_

#### Manages errors for third-party modules ####

Version: 0.0.6

When you're writing a third-party module you'll probably deal with errors. If you want to send custom errors to your users you should send a code and then in your documentation explain these codes.

This module eases the error management, you only need to give a number, a code and a description and when you need them just get and throw them.

The built-in errors are also available so if you need to manually throw a `EEXIST` you can do so.

The error description accepts variables, that is, you can create an error with a variable and when you get it you can set the variable value:

```javascript
ep.create (1000, "MY_CUSTOM_ERROR", "This is a {which} error");
console.log (ep.get ("MY_CUSTOM_ERROR", { which: "custom" }));

/*
Prints:

{ [MY_CUSTOM_ERROR: This is a custom error] errno: 1000, code: "MY_CUSTOM_ERROR" }
*/
```

If you want to add another property different from `errno`, `code` and `description` you can do it, just pass an object with the new properties (if the property value is a string it can also have variables):

```javascript
ep.create (ep.next (), "MY_CUSTOM_ERROR", "This is a {which} error", {
	path: "{path}",
	fn: function (){
		console.log ("custom error");
	}
});
var error = ep.get ("MY_CUSTOM_ERROR", { which: "custom", path: "some/path" });
error.fn ();
console.log (error);

/*
Prints:

custom error
{ [MY_CUSTOM_ERROR: This is a custom error]
  errno: 100,
  code: "MY_CUSTOM_ERROR",
  path: "some/path",
  fn: [Function] }
*/
```

#### Installation ####

```
npm install error-provider
```

#### Example ####

```javascript
var ep = require ("error-provider");

console.log (ep.get (ep.ENOENT));

var n = ep.next ();
ep.create (n, "TEST1", "test 1");
console.log (ep.get (ep.TEST1));
console.log (ep.get ("TEST1"));
console.log (ep.get (n));

/*
Prints:

{ [ENOENT: no such file or directory] errno: 34, code: "ENOENT" }
{ [TEST1: test 1] errno: 100, code: "TEST1" }
{ [TEST1: test 1] errno: 100, code: "TEST1" }
{ [TEST1: test 1] errno: 100, code: "TEST1" }
*/
```

#### Methods ####

There's a special method called `local()` that returns a local error provider. If you need a local storage with custom errors that you don't need to make public, `local()` can help you. If another third-party module uses the `error-provider` module you won't see its errors, the local error provider is local to your module. The first available errno is 0 and the set of default codes is empty, that is, you can't return a built-in error like `EEXIST`. You can create all the storages you want.

```javascript
var ep = require ("error-provider").local ();
ep.create (ep.next (), ...);
ep.get (...);
```

- [ep.create(errno, code, description[, properties])](#create)
- [ep.get(id[, vars])](#get)
- [ep.next()](#next)

<a name="create"></a>
__ep.create(errno, code, description[, properties])__  
Creates an error with an id, code, description and additional properties.

The strings can contain variables that can be set later. A variable is a name encapsulated inside curly braces:

```javascript
ep.create (ep.next (), "TEST1", "test 1", {
	path: "{p}"
});
console.log (ep.get ("TEST1", { p: "some/path" }));

/*
Prints:

{ [TEST1: test 1] errno: 100, code: "TEST1", path: "some/path" }
*/
```

<a name="get"></a>
__ep.get(id[, vars])__  
Returns an error. It can be a Node.js built-in error such as `ENOENT` or one that was created previously.

The `id` can be a Number, String or Object:

```javascript
ep.create (ep.next (), "TEST1", "Test 1");
console.log (ep.get (ep.TEST1));
console.log (ep.get ("TEST1"));
console.log (ep.get (100));
```

If `vars` is provided, the variables found in the error properties that are String will be set with the given value.

```javascript
ep.create (ep.next (), "TEST1", "test {number}", {
	n: "{number}",
	p: "{value}"
});
console.log (ep.get ("TEST1", { number: 1, value: "ok" }));

/*
Prints:

{ [TEST1: test 1]
  errno: 100,
  code: "TEST1",
  n: "1",
  p: "ok" }
*/
```

<a name="next"></a>
__ep.next()__  
Returns the next available error number and increments the counter. This number is an identifier to create errors.

The first available number starts at 100.

The following example creates three errors with identified with 100, 101 and 102.

```javascript
ep.create (ep.next (), ...);
ep.create (ep.next (), ...);
ep.create (ep.next (), ...);
```

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