You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.8 KiB

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