# json-api-error-handler

> Express error handler that return JSON error description to client.

Latest version **1.0.4** (published 2019-09-26) · ISC license · 0 weekly downloads

## Install

```sh
npm install json-api-error-handler
pnpm add json-api-error-handler
yarn add json-api-error-handler
bun add json-api-error-handler
```

## 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.4 |
| Published | 2019-09-26 |
| First published | 2019-09-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Samuele Battistella |
| Maintainers | battistar |
| Keywords | express, json, error, handler, client, http |

## Links

- npm: https://www.npmjs.com/package/json-api-error-handler
- Repository: https://github.com/Battistar/json-api-error-handler
- Homepage: https://github.com/Battistar/json-api-error-handler#readme
- Issues: https://github.com/Battistar/json-api-error-handler/issues
- npm.io page: https://npm.io/package/json-api-error-handler

## Dependencies (1)

- [esm](https://npm.io/package/esm.md) ^3.2.25

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.4 (latest) — 2019-09-26
- 1.0.3 — 2019-09-26
- 1.0.2 — 2019-09-26
- 1.0.1 — 2019-09-26

## README

# json-api-error-handler
Express error handler that return JSON error description to client.

## Installation
Install package with `npm`:
```
npm i json-api-error-handler
```

## Usage
Import `jsonAPIErrorHandler` middleware and define as last, after other app.use() and routes calls:
```javascript
import cookieParser from 'cookie-parser'
import logger from 'morgan'
import jsonAPIErrorHandler from 'json-api-error-handler'

const app = express()

app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(jsonAPIErrorHandler) // <- Last middleware
```

In your routes you can call `next()` function, passing anything to argument (except the string 'route'), or `throw` an error. 
Express regards the current request as being an error and will skip any remaining non-error handling routing and middleware 
functions, and call error handling middleware:
```javascript
app.get('/', function (req, res, next) {
  // Some code...
  next('This is an error')
  // Some code...
})
```

The error handler send to client a JSON object with fields **code**, **status**, **description**.
```javascript
{
  code: 500,
  status: 'Internal Server Error',
  description: 'This is an error'
}
```

If the `next()` function argument is an `Error` object, in a *development* enviornment, the **description** field contains 
a stack trace of the error, while in *production* it contains only the error title.

#### HTTPError
You can use `HTTPError` class to customize JSON response with an HTTP status code:
```javascript
app.get('/', function (req, res, next) {
  // Some code...
  next(new HTTPError(404, 'User not found'))
  // Some code...
})
```

```javascript
{
  code: 404,
  status: 'Not Found',
  description: 'User not found'
}
```

## License
This project is licensed under the MIT license. See the [LICENSE](LICENSE) file for more info.

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