# httperrors

> Handy JavaScript Error classes representing the standard HTTP errors. Easily extensible and subclassable.

Latest version **2.3.0** (published 2017-08-24) · 0 weekly downloads

## Install

```sh
npm install httperrors
pnpm add httperrors
yarn add httperrors
bun add httperrors
```

## 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 | 2.3.0 |
| Published | 2017-08-24 |
| First published | 2012-02-23 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.2.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 31 |
| Author | Andreas Lind Petersen |
| Maintainers | papandreou, ath, maartenwinter |
| Keywords | http, errors, error, request, proxy, client, class, errorclass |

## Links

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

## Dependencies (1)

- [createerror](https://npm.io/package/createerror.md) 1.3.0

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 2.3.0 (latest) — 2017-08-24
- 2.2.0 — 2017-05-01
- 2.1.0 — 2016-04-08
- 2.0.1 — 2016-02-21
- 2.0.0 — 2016-02-21
- 1.2.0 — 2016-01-06
- 1.1.0 — 2015-07-22
- 1.0.0 — 2015-07-21
- 0.7.1 — 2014-12-01
- 0.7.0 — 2014-09-22
- 0.6.1 — 2014-09-09
- 0.6.0 — 2014-09-09
- 0.5.0 — 2014-07-24
- 0.4.1 — 2014-04-15
- 0.4.0 — 2013-09-06
- … 5 more at https://npm.io/package/httperrors/versions

## README

node-httperrors
===============

Exposes HTTP 4xx and 5xx status codes as JavaScript Error objects. The error classes are created using the <a href="https://github.com/One-com/node-createerror">createError module</a>.

The original use case for `httpErrors` is to use a custom <a href="https://github.com/visionmedia/express">express</a> error handler that uses the `statusCode` property of the error instance as the status code for the response, and optionally logs further info from the error.


Installation
------------

Make sure you have node.js and npm installed, then run:

    npm install httperrors

Usage
-----

    var httpErrors = require('httperrors');

    // Instatiate by status code:
    var myError = httpErrors(412);

    // Instantiate by name (UpperCamelCase):
    var err = new httpErrors.NotFound('The thing you were looking for was not found');

    console.warn(err.toString()); // NotFound [404]: The thing you were looking for was not found

    if (identityCrisis) {
        throw new httpErrors.ImATeapot('Dude...');
    }

The CamelCased error name is exposed as a true property on the
instances, so your error handling code becomes quite readable (and you
can avoid using instanceof):

    if (err.NotFound) {
        // ...
    } else if (err.BadGateway) {
        // ...
    }

You can also create an error by status code (useful when proxying):

    function fetchSomething(cb) {
        var request = require('request');
        request('http://example.com/thething', function (err, response) {
            if (err) {
                return cb(new (httpErrors[response.statusCode] || httpErrors.BadGateway)());
            }
            // ...
        });
    }

License
-------

3-clause BSD license -- see the `LICENSE` file for details.

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