414397766b21d14660f435f65cb43b52ab08da7e
[aai/esr-gui.git] /
1 # http-errors
2
3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Node.js Version][node-version-image]][node-version-url]
6 [![Build Status][travis-image]][travis-url]
7 [![Test Coverage][coveralls-image]][coveralls-url]
8
9 Create HTTP errors for Express, Koa, Connect, etc. with ease.
10
11 ## Install
12
13 This is a [Node.js](https://nodejs.org/en/) module available through the
14 [npm registry](https://www.npmjs.com/). Installation is done using the
15 [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
16
17 ```bash
18 $ npm install http-errors
19 ```
20
21 ## Example
22
23 ```js
24 var createError = require('http-errors')
25 var express = require('express')
26 var app = express()
27
28 app.use(function (req, res, next) {
29   if (!req.user) return next(createError(401, 'Please login to view this page.'))
30   next()
31 })
32 ```
33
34 ## API
35
36 This is the current API, currently extracted from Koa and subject to change.
37
38 All errors inherit from JavaScript `Error` and the exported `createError.HttpError`.
39
40 ### Error Properties
41
42 - `expose` - can be used to signal if `message` should be sent to the client,
43   defaulting to `false` when `status` >= 500
44 - `headers` - can be an object of header names to values to be sent to the
45   client, defaulting to `undefined`. When defined, the key names should all
46   be lower-cased
47 - `message`
48 - `status` and `statusCode` - the status code of the error, defaulting to `500`
49
50 ### createError([status], [message], [properties])
51
52 <!-- eslint-disable no-undef, no-unused-vars -->
53
54 ```js
55 var err = createError(404, 'This video does not exist!')
56 ```
57
58 - `status: 500` - the status code as a number
59 - `message` - the message of the error, defaulting to node's text for that status code.
60 - `properties` - custom properties to attach to the object
61
62 ### new createError\[code || name\](\[msg]\))
63
64 <!-- eslint-disable no-undef, no-unused-vars -->
65
66 ```js
67 var err = new createError.NotFound()
68 ```
69
70 - `code` - the status code as a number
71 - `name` - the name of the error as a "bumpy case", i.e. `NotFound` or `InternalServerError`.
72
73 #### List of all constructors
74
75 |Status Code|Constructor Name             |
76 |-----------|-----------------------------|
77 |400        |BadRequest                   |
78 |401        |Unauthorized                 |
79 |402        |PaymentRequired              |
80 |403        |Forbidden                    |
81 |404        |NotFound                     |
82 |405        |MethodNotAllowed             |
83 |406        |NotAcceptable                |
84 |407        |ProxyAuthenticationRequired  |
85 |408        |RequestTimeout               |
86 |409        |Conflict                     |
87 |410        |Gone                         |
88 |411        |LengthRequired               |
89 |412        |PreconditionFailed           |
90 |413        |PayloadTooLarge              |
91 |414        |URITooLong                   |
92 |415        |UnsupportedMediaType         |
93 |416        |RangeNotSatisfiable          |
94 |417        |ExpectationFailed            |
95 |418        |ImATeapot                    |
96 |421        |MisdirectedRequest           |
97 |422        |UnprocessableEntity          |
98 |423        |Locked                       |
99 |424        |FailedDependency             |
100 |425        |UnorderedCollection          |
101 |426        |UpgradeRequired              |
102 |428        |PreconditionRequired         |
103 |429        |TooManyRequests              |
104 |431        |RequestHeaderFieldsTooLarge  |
105 |451        |UnavailableForLegalReasons   |
106 |500        |InternalServerError          |
107 |501        |NotImplemented               |
108 |502        |BadGateway                   |
109 |503        |ServiceUnavailable           |
110 |504        |GatewayTimeout               |
111 |505        |HTTPVersionNotSupported      |
112 |506        |VariantAlsoNegotiates        |
113 |507        |InsufficientStorage          |
114 |508        |LoopDetected                 |
115 |509        |BandwidthLimitExceeded       |
116 |510        |NotExtended                  |
117 |511        |NetworkAuthenticationRequired|
118
119 ## License
120
121 [MIT](LICENSE)
122
123 [npm-image]: https://img.shields.io/npm/v/http-errors.svg
124 [npm-url]: https://npmjs.org/package/http-errors
125 [node-version-image]: https://img.shields.io/node/v/http-errors.svg
126 [node-version-url]: https://nodejs.org/en/download/
127 [travis-image]: https://img.shields.io/travis/jshttp/http-errors.svg
128 [travis-url]: https://travis-ci.org/jshttp/http-errors
129 [coveralls-image]: https://img.shields.io/coveralls/jshttp/http-errors.svg
130 [coveralls-url]: https://coveralls.io/r/jshttp/http-errors
131 [downloads-image]: https://img.shields.io/npm/dm/http-errors.svg
132 [downloads-url]: https://npmjs.org/package/http-errors