# assert-err

> assertion library that throws user-specified error types (accepts error class and message)

Latest version **1.1.0** (published 2016-09-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install assert-err
pnpm add assert-err
yarn add assert-err
bun add assert-err
```

## 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.1.0 |
| Published | 2016-09-13 |
| First published | 2016-07-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Tejesh Mehta |
| Maintainers | tjmehta |
| Keywords | assert, error, type, class, custom, user, constructor |

## Links

- npm: https://www.npmjs.com/package/assert-err
- Repository: https://github.com/tjmehta/assert-err
- Issues: https://github.com/tjmehta/assert-err/issues
- npm.io page: https://npm.io/package/assert-err

## Dependencies (2)

- [object-assign](https://npm.io/package/object-assign.md) ^4.1.0
- [escape-string-regexp](https://npm.io/package/escape-string-regexp.md) ^1.0.5

## 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.1.0 (latest) — 2016-09-13
- 1.0.0 — 2016-07-19

## README

# assert-err
assertion library that throws user-specified error types (accepts error class and message)

# Installation
```bash
npm i --save assert-err
```

# Usage
#### Example 1: full example
```js
// example-1.js
var assertErr = require('assert-err')

function add (a, b) {
  assertErr(typeof a === 'number', TypeError, '"a" must be a number')
  assertErr(typeof b === 'number', TypeError, '"b" must be a number') //  line 6
  return a + b
}

add(1, 'no') // line 10
/*
TypeError: "b" must be a number
    at add (example-1.js:6:3)
    at Object.<anonymous> (example-1.js:10:1)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:139:18)
    at node.js:999:3
*/
```
#### Example 2: Error props
If using global.Error, assertErr accepts a `props` argument to extend `error`
```js
assertErr(false, Error, 'message', { code: 1, status: 'status' }) // 0 args
// { [Error: boom] code: 1, status: 'status' }
```
#### Example 3: args
assertErr supports up to 5 err constructor args
```js
assertErr(false, CustomError) // 0 args
assertErr(false, CustomError, 'foo') // 1 args
assertErr(false, CustomError, 'foo', 'bar') // 2 args
assertErr(false, CustomError, 'foo', 'bar', 'qux') // 3 args
assertErr(false, CustomError, 'foo', 'bar', 'qux', 'corge') // 4 args
assertErr(false, CustomError, 'foo', 'bar', 'qux', 'corge', 'yolo') // 5 args
// error...
assertErr(false, CustomError, 'foo', 'bar', 'qux', 'corge', 'yolo', 'toomany') // 6 args
// Error: assertErr does not support more than five Error args
```

# License
MIT

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