# node-exceptions

> Extendable error class for nodejs to extend native errors

Latest version **4.0.1** (published 2018-10-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-exceptions
pnpm add node-exceptions
yarn add node-exceptions
bun add node-exceptions
```

## 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 | 4.0.1 |
| Published | 2018-10-20 |
| First published | 2016-02-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 29 |
| Author | amanvirk,adonisjs |
| Maintainers | virk |
| Keywords | exception, node-errors, custom-errors, Error |

## Links

- npm: https://www.npmjs.com/package/node-exceptions
- Repository: https://github.com/poppinss/node-exceptions
- Homepage: https://github.com/poppinss/node-exceptions#readme
- Issues: https://github.com/poppinss/node-exceptions/issues
- npm.io page: https://npm.io/package/node-exceptions

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

- 4.0.1 (latest) — 2018-10-20
- 4.0.0 — 2018-10-20
- 3.0.0 — 2018-01-15
- 2.0.2 — 2017-06-12
- 2.0.1 — 2017-04-02
- 2.0.0 — 2017-03-15
- 1.0.3 — 2016-08-08
- 1.0.2 — 2016-08-05
- 1.0.1 — 2016-02-05
- 1.0.0 — 2016-02-04

## README

# Node Exceptions

[![NPM Version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Appveyor][appveyor-image]][appveyor-url]
[![Coveralls][coveralls-image]][coveralls-url]

<a href="http://i1117.photobucket.com/albums/k594/thetutlage/poppins-1_zpsg867sqyl.png">
<img src="http://i1117.photobucket.com/albums/k594/thetutlage/poppins-1_zpsg867sqyl.png" width="300px" align="right" vspace="20px" />
</a>

Throwing errors in Javascript does not give much information about the error type as it is really hard to throw custom exceptions. Node Exceptions is a tiny wrapper which will let you extend the Error class and throw custom errors.

## Why custom errors
Errors are thrown anywhere inside the code and handling them properly is required. For example you have an HTTP application, which can throw multiple errors and in order to handle those errors gracefully, you need to know the error types or their names.

```javascript
switch (err.name) {
  case 'HttpException':
    // do something
  case 'RunTimeException':
    // do something else
}
```


## Install

```bash
npm i --save node-exceptions
```

## Creating custom errors

```javascript
const NE = require('node-exceptions')

class MyCustomError extends NE.LogicalException {}

try {
  throw new MyCustomError('Something bad happened')
} catch (e) {
  console.log(e.status) // equals 500
  console.log(e.name) // equals MyCustomError
  console.log(e.message) // Something bad happened
  console.log(e.stack) // Error stack with correct reference to filepath and linenum
  console.log(e.toString()) // MyCustomError: Something bad happened
}
```

## Custom error status
It is also possible to have a custom error status when throwing exceptions.

```javascript
const NE = require('node-exceptions')

class HttpException extends NE.LogicalException {}

try {
  throw new HttpException('Page not found', 404)
} catch (e) {
  console.log(e.status) // equals 404
}
```

## API Docs
Access complete API Docs [here](https://poppinss.github.io/node-exceptions/docs/index.html)

[appveyor-image]: https://img.shields.io/appveyor/ci/thetutlage/node-exceptions/master.svg?style=flat-square

[appveyor-url]: https://ci.appveyor.com/project/thetutlage/node-exceptions

[npm-image]: https://img.shields.io/npm/v/node-exceptions.svg?style=flat-square
[npm-url]: https://npmjs.org/package/node-exceptions

[travis-image]: https://img.shields.io/travis/poppinss/node-exceptions/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/poppinss/node-exceptions

[coveralls-image]: https://img.shields.io/coveralls/poppinss/node-exceptions/develop.svg?style=flat-square

[coveralls-url]: https://coveralls.io/github/poppinss/node-exceptions%

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