• Stars
    star
    1,489
  • Rank 31,555 (Top 0.7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Create HTTP Errors

http-errors

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Create HTTP errors for Express, Koa, Connect, etc. with ease.

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install http-errors

Example

var createError = require('http-errors')
var express = require('express')
var app = express()

app.use(function (req, res, next) {
  if (!req.user) return next(createError(401, 'Please login to view this page.'))
  next()
})

API

This is the current API, currently extracted from Koa and subject to change.

Error Properties

  • expose - can be used to signal if message should be sent to the client, defaulting to false when status >= 500
  • headers - can be an object of header names to values to be sent to the client, defaulting to undefined. When defined, the key names should all be lower-cased
  • message - the traditional error message, which should be kept short and all single line
  • status - the status code of the error, mirroring statusCode for general compatibility
  • statusCode - the status code of the error, defaulting to 500

createError([status], [message], [properties])

Create a new error object with the given message msg. The error object inherits from createError.HttpError.

var err = createError(404, 'This video does not exist!')
  • status: 500 - the status code as a number
  • message - the message of the error, defaulting to node's text for that status code.
  • properties - custom properties to attach to the object

createError([status], [error], [properties])

Extend the given error object with createError.HttpError properties. This will not alter the inheritance of the given error object, and the modified error object is the return value.

fs.readFile('foo.txt', function (err, buf) {
  if (err) {
    if (err.code === 'ENOENT') {
      var httpError = createError(404, err, { expose: false })
    } else {
      var httpError = createError(500, err)
    }
  }
})
  • status - the status code as a number
  • error - the error object to extend
  • properties - custom properties to attach to the object

createError.isHttpError(val)

Determine if the provided val is an HttpError. This will return true if the error inherits from the HttpError constructor of this module or matches the "duck type" for an error this module creates. All outputs from the createError factory will return true for this function, including if an non-HttpError was passed into the factory.

new createError[code || name]([msg]))

Create a new error object with the given message msg. The error object inherits from createError.HttpError.

var err = new createError.NotFound()
  • code - the status code as a number
  • name - the name of the error as a "bumpy case", i.e. NotFound or InternalServerError.

List of all constructors

Status Code Constructor Name
400 BadRequest
401 Unauthorized
402 PaymentRequired
403 Forbidden
404 NotFound
405 MethodNotAllowed
406 NotAcceptable
407 ProxyAuthenticationRequired
408 RequestTimeout
409 Conflict
410 Gone
411 LengthRequired
412 PreconditionFailed
413 PayloadTooLarge
414 URITooLong
415 UnsupportedMediaType
416 RangeNotSatisfiable
417 ExpectationFailed
418 ImATeapot
421 MisdirectedRequest
422 UnprocessableEntity
423 Locked
424 FailedDependency
425 TooEarly
426 UpgradeRequired
428 PreconditionRequired
429 TooManyRequests
431 RequestHeaderFieldsTooLarge
451 UnavailableForLegalReasons
500 InternalServerError
501 NotImplemented
502 BadGateway
503 ServiceUnavailable
504 GatewayTimeout
505 HTTPVersionNotSupported
506 VariantAlsoNegotiates
507 InsufficientStorage
508 LoopDetected
509 BandwidthLimitExceeded
510 NotExtended
511 NetworkAuthenticationRequired

License

MIT

More Repositories

1

mime-types

The ultimate javascript content-type utility.
JavaScript
1,311
star
2

cookie

HTTP server cookie parsing and serialization
JavaScript
1,303
star
3

mime-db

Media Type Database
JavaScript
1,083
star
4

basic-auth

Generic basic auth Authorization header field parser
JavaScript
704
star
5

on-finished

Execute a callback when a request closes, finishes, or errors
JavaScript
389
star
6

negotiator

An HTTP content negotiator for Node.js
JavaScript
307
star
7

statuses

HTTP status utility
JavaScript
269
star
8

accepts

Higher-level content negotiation
JavaScript
252
star
9

etag

Create simple HTTP ETags
JavaScript
251
star
10

type-is

Infer the content-type of a request.
JavaScript
226
star
11

content-disposition

Create and parse HTTP Content-Disposition header
JavaScript
220
star
12

methods

HTTP verbs that node supports
JavaScript
177
star
13

fresh

HTTP request freshness testing
JavaScript
159
star
14

on-headers

Execute a listener when a response is about to write headers.
JavaScript
154
star
15

http-assert

assert with status codes
JavaScript
151
star
16

proxy-addr

Determine address of proxied request
JavaScript
132
star
17

content-type

Create and parse HTTP Content-Type header
JavaScript
130
star
18

style-guide

jshttp style guide
JavaScript
108
star
19

compressible

Compressible Content-Type / mime checking.
JavaScript
98
star
20

range-parser

Range header field parser
JavaScript
90
star
21

vary

Manipulate the HTTP Vary header
JavaScript
61
star
22

forwarded

Parse HTTP X-Forwarded-For header
JavaScript
56
star
23

media-typer

Simple RFC 6838 media type parser and formatter
JavaScript
54
star
24

jshttp.github.io

HTML
47
star
25

spdy-push

SPDY Push helper - will by replaced by http-push
JavaScript
29
star
26

http-push

14
star
27

http-utils

Low-level HTTP parsing/formatting utilities
JavaScript
14
star
28

.github

1
star