# express-json-errors

> Express middleware to handle errors on JSON APIs

Latest version **1.0.5** (published 2017-04-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install express-json-errors
pnpm add express-json-errors
yarn add express-json-errors
bun add express-json-errors
```

## 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.5 |
| Published | 2017-04-11 |
| First published | 2017-02-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jason Brodie |
| Maintainers | jason0518 |

## Links

- npm: https://www.npmjs.com/package/express-json-errors
- Repository: https://github.com/idavatars-engineering/express-json-errors
- Homepage: https://github.com/idavatars-engineering/express-json-errors#readme
- Issues: https://github.com/idavatars-engineering/express-json-errors/issues
- npm.io page: https://npm.io/package/express-json-errors

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) ^4.17.4
- [supertest](https://npm.io/package/supertest.md) ^3.0.0

## Recent versions

- 1.0.5 (latest) — 2017-04-11
- 1.0.4 — 2017-02-06
- 1.0.3 — 2017-02-06
- 1.0.2 — 2017-02-06
- 1.0.1 — 2017-02-06
- 1.0.0 — 2017-02-06

## README

# express-json-errors

[![npm version](https://img.shields.io/npm/v/express-json-errors.svg)](https://www.npmjs.com/package/express-json-errors)

An error handler for JSON APIs. They follow the general 'spec' outlined [here](http://jsonapi.org/examples/#error-objects)

## Example
```js
var express     = require('express'),
errorHandler    = require('express-json-errors');

var app = new Express();
app.use(errorHandler());

// An simple example "route" or middleware
app.use((req, res) => {
    res.error('This is a JSON error!');

    /**
    * This will output as JSON to the response
    *   {
    *       code: 400,
    *       errors: [
    *           {
    *               code: 400,
    *               title: 'This is a JSON error!',
    *               description: 'n/a'
    *           }
    *       ]
    *   }
    */
});
```

## API
By default you have `res.error` to use. You can send it Strings, plain Objects, Errors, Arrays (containing any combination of Strings, Objects, or Errors).
```js
// Using strings
app.use((req, res) => {
    res.error('This is a JSON error!');
});

// Using Objects
app.use((req, res) => {
    res.error({
        code: 404,
        title: 'That resource was not found',
        description: 'I could not find the resource `my_resource`.'
    });
});

// Using Errors
app.use((req, res) => {
    res.error(new Error('This is an error!'));
});

// Using Arrays
app.use((req, res) => {
    res.error([
        'You did something wrong',
        new Error('This is an error!'),
        {
            title: 'This is really not good!'
        }
    ]);
});
```

There is also support for [express-validator](https://github.com/ctavan/express-validator). You can use it like:
```js
app.use((req, res) => {
    req.checkBody('my_input', 'Missing my_input parameter').notEmpty();

    req.getValidationResult()
        .then(result => {
            if(!result.isEmpty()) return res.validationError(result.array());
        })
        .catch(res.error);
});
```

## Status Codes
The API has a somewhat semantic way of handling errors. If you only have on error, the default error code is 400 unless otherwise specified. So for example:
```js
// A single error
app.use((req, res) => {
    res.error('This is a JSON error!'); // Will result in a response code of 400
});

// A single error
app.use((req, res) => {
    res.error({
        code: 404,
        title: 'This is a JSON error!'
    }); // Will result in a response code of 404
});
```

If you have multiple errors (aka an array of errors) the system will default to 400. Then each error object in the response will have it's own status code.

If you use a JS error, the status code will be 500. For example:
```js
// A single error
app.use((req, res) => {
    res.error(new Error('This is an error!')); // Will result in a response code of 500
});
```

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