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.

135 lines
4.7 KiB

  1. # http-errors
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Node.js Version][node-version-image]][node-version-url]
  5. [![Build Status][travis-image]][travis-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. ```bash
  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. All errors inherit from JavaScript `Error` and the exported `createError.HttpError`.
  28. ### Error Properties
  29. - `expose` - can be used to signal if `message` should be sent to the client,
  30. defaulting to `false` when `status` >= 500
  31. - `headers` - can be an object of header names to values to be sent to the
  32. client, defaulting to `undefined`. When defined, the key names should all
  33. be lower-cased
  34. - `message` - the traditional error message, which should be kept short and all
  35. single line
  36. - `status` - the status code of the error, mirroring `statusCode` for general
  37. compatibility
  38. - `statusCode` - the status code of the error, defaulting to `500`
  39. ### createError([status], [message], [properties])
  40. <!-- eslint-disable no-undef, no-unused-vars -->
  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. ### new createError\[code || name\](\[msg]\))
  48. <!-- eslint-disable no-undef, no-unused-vars -->
  49. ```js
  50. var err = new createError.NotFound()
  51. ```
  52. - `code` - the status code as a number
  53. - `name` - the name of the error as a "bumpy case", i.e. `NotFound` or `InternalServerError`.
  54. #### List of all constructors
  55. |Status Code|Constructor Name |
  56. |-----------|-----------------------------|
  57. |400 |BadRequest |
  58. |401 |Unauthorized |
  59. |402 |PaymentRequired |
  60. |403 |Forbidden |
  61. |404 |NotFound |
  62. |405 |MethodNotAllowed |
  63. |406 |NotAcceptable |
  64. |407 |ProxyAuthenticationRequired |
  65. |408 |RequestTimeout |
  66. |409 |Conflict |
  67. |410 |Gone |
  68. |411 |LengthRequired |
  69. |412 |PreconditionFailed |
  70. |413 |PayloadTooLarge |
  71. |414 |URITooLong |
  72. |415 |UnsupportedMediaType |
  73. |416 |RangeNotSatisfiable |
  74. |417 |ExpectationFailed |
  75. |418 |ImATeapot |
  76. |421 |MisdirectedRequest |
  77. |422 |UnprocessableEntity |
  78. |423 |Locked |
  79. |424 |FailedDependency |
  80. |425 |UnorderedCollection |
  81. |426 |UpgradeRequired |
  82. |428 |PreconditionRequired |
  83. |429 |TooManyRequests |
  84. |431 |RequestHeaderFieldsTooLarge |
  85. |451 |UnavailableForLegalReasons |
  86. |500 |InternalServerError |
  87. |501 |NotImplemented |
  88. |502 |BadGateway |
  89. |503 |ServiceUnavailable |
  90. |504 |GatewayTimeout |
  91. |505 |HTTPVersionNotSupported |
  92. |506 |VariantAlsoNegotiates |
  93. |507 |InsufficientStorage |
  94. |508 |LoopDetected |
  95. |509 |BandwidthLimitExceeded |
  96. |510 |NotExtended |
  97. |511 |NetworkAuthenticationRequired|
  98. ## License
  99. [MIT](LICENSE)
  100. [npm-image]: https://img.shields.io/npm/v/http-errors.svg
  101. [npm-url]: https://npmjs.org/package/http-errors
  102. [node-version-image]: https://img.shields.io/node/v/http-errors.svg
  103. [node-version-url]: https://nodejs.org/en/download/
  104. [travis-image]: https://img.shields.io/travis/jshttp/http-errors.svg
  105. [travis-url]: https://travis-ci.org/jshttp/http-errors
  106. [coveralls-image]: https://img.shields.io/coveralls/jshttp/http-errors.svg
  107. [coveralls-url]: https://coveralls.io/r/jshttp/http-errors
  108. [downloads-image]: https://img.shields.io/npm/dm/http-errors.svg
  109. [downloads-url]: https://npmjs.org/package/http-errors