3 [![NPM Version][npm-version-image]][npm-url]
4 [![NPM Downloads][npm-downloads-image]][node-url]
5 [![Node.js Version][node-image]][node-url]
6 [![Build Status][travis-image]][travis-url]
7 [![Test Coverage][coveralls-image]][coveralls-url]
9 Create HTTP errors for Express, Koa, Connect, etc. with ease.
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):
18 $ npm install http-errors
24 var createError = require('http-errors')
25 var express = require('express')
28 app.use(function (req, res, next) {
29 if (!req.user) return next(createError(401, 'Please login to view this page.'))
36 This is the current API, currently extracted from Koa and subject to change.
40 - `expose` - can be used to signal if `message` should be sent to the client,
41 defaulting to `false` when `status` >= 500
42 - `headers` - can be an object of header names to values to be sent to the
43 client, defaulting to `undefined`. When defined, the key names should all
45 - `message` - the traditional error message, which should be kept short and all
47 - `status` - the status code of the error, mirroring `statusCode` for general
49 - `statusCode` - the status code of the error, defaulting to `500`
51 ### createError([status], [message], [properties])
53 Create a new error object with the given message `msg`.
54 The error object inherits from `createError.HttpError`.
56 <!-- eslint-disable no-undef, no-unused-vars -->
59 var err = createError(404, 'This video does not exist!')
62 - `status: 500` - the status code as a number
63 - `message` - the message of the error, defaulting to node's text for that status code.
64 - `properties` - custom properties to attach to the object
66 ### createError([status], [error], [properties])
68 Extend the given `error` object with `createError.HttpError`
69 properties. This will not alter the inheritance of the given
70 `error` object, and the modified `error` object is the
73 <!-- eslint-disable no-redeclare, no-undef, no-unused-vars -->
76 fs.readFile('foo.txt', function (err, buf) {
78 if (err.code === 'ENOENT') {
79 var httpError = createError(404, err, { expose: false })
81 var httpError = createError(500, err)
87 - `status` - the status code as a number
88 - `error` - the error object to extend
89 - `properties` - custom properties to attach to the object
91 ### new createError\[code || name\](\[msg]\))
93 Create a new error object with the given message `msg`.
94 The error object inherits from `createError.HttpError`.
96 <!-- eslint-disable no-undef, no-unused-vars -->
99 var err = new createError.NotFound()
102 - `code` - the status code as a number
103 - `name` - the name of the error as a "bumpy case", i.e. `NotFound` or `InternalServerError`.
105 #### List of all constructors
107 |Status Code|Constructor Name |
108 |-----------|-----------------------------|
111 |402 |PaymentRequired |
114 |405 |MethodNotAllowed |
115 |406 |NotAcceptable |
116 |407 |ProxyAuthenticationRequired |
117 |408 |RequestTimeout |
120 |411 |LengthRequired |
121 |412 |PreconditionFailed |
122 |413 |PayloadTooLarge |
124 |415 |UnsupportedMediaType |
125 |416 |RangeNotSatisfiable |
126 |417 |ExpectationFailed |
128 |421 |MisdirectedRequest |
129 |422 |UnprocessableEntity |
131 |424 |FailedDependency |
132 |425 |UnorderedCollection |
133 |426 |UpgradeRequired |
134 |428 |PreconditionRequired |
135 |429 |TooManyRequests |
136 |431 |RequestHeaderFieldsTooLarge |
137 |451 |UnavailableForLegalReasons |
138 |500 |InternalServerError |
139 |501 |NotImplemented |
141 |503 |ServiceUnavailable |
142 |504 |GatewayTimeout |
143 |505 |HTTPVersionNotSupported |
144 |506 |VariantAlsoNegotiates |
145 |507 |InsufficientStorage |
147 |509 |BandwidthLimitExceeded |
149 |511 |NetworkAuthenticationRequired|
155 [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/http-errors/master
156 [coveralls-url]: https://coveralls.io/r/jshttp/http-errors?branch=master
157 [node-image]: https://badgen.net/npm/node/http-errors
158 [node-url]: https://nodejs.org/en/download
159 [npm-downloads-image]: https://badgen.net/npm/dm/http-errors
160 [npm-url]: https://npmjs.org/package/http-errors
161 [npm-version-image]: https://badgen.net/npm/v/http-errors
162 [travis-image]: https://badgen.net/travis/jshttp/http-errors/master
163 [travis-url]: https://travis-ci.org/jshttp/http-errors