Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

http-errors

jshttp177.9mMIT2.0.0TypeScript support: definitely-typed

Create HTTP error objects

http, error

readme

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

changelog

2.0.0 / 2021-12-17

  • Drop support for Node.js 0.6
  • Remove I'mateapot export; use ImATeapot instead
  • Remove support for status being non-first argument
  • Rename UnorderedCollection constructor to TooEarly
  • deps: depd@2.0.0
    • Replace internal eval usage with Function constructor
    • Use instance methods on process to check for listeners
  • deps: statuses@2.0.1
    • Fix messaging casing of 418 I'm a Teapot
    • Remove code 306
    • Rename 425 Unordered Collection to standard 425 Too Early

2021-11-14 / 1.8.1

2020-06-29 / 1.8.0

2019-06-24 / 1.7.3

2019-02-18 / 1.7.2

2018-09-08 / 1.7.1

  • Fix error creating objects in some environments

2018-07-30 / 1.7.0

  • Set constructor name when possible
  • Use toidentifier module to make class names
  • deps: statuses@'>= 1.5.0 < 2'

2018-03-29 / 1.6.3

  • deps: depd@~1.1.2
    • perf: remove argument reassignment
  • deps: setprototypeof@1.1.0
  • deps: statuses@'>= 1.4.0 < 2'

2017-08-04 / 1.6.2

  • deps: depd@1.1.1
    • Remove unnecessary Buffer loading

2017-02-20 / 1.6.1

2017-02-14 / 1.6.0

  • Accept custom 4xx and 5xx status codes in factory
  • Add deprecation message to "I'mateapot" export
  • Deprecate passing status code as anything except first argument in factory
  • Deprecate using non-error status codes
  • Make message property enumerable for HttpErrors

2016-11-16 / 1.5.1

2016-05-18 / 1.5.0

  • Support new code 421 Misdirected Request
  • Use setprototypeof module to replace __proto__ setting
  • deps: statuses@'>= 1.3.0 < 2'
    • Add 421 Misdirected Request
    • perf: enable strict mode
  • perf: enable strict mode

2016-01-28 / 1.4.0

  • Add HttpError export, for err instanceof createError.HttpError
  • deps: inherits@2.0.1
  • deps: statuses@'>= 1.2.1 < 2'
    • Fix message for status 451
    • Remove incorrect nginx status code

2015-02-02 / 1.3.1

  • Fix regression where status can be overwritten in createError props

2015-02-01 / 1.3.0

  • Construct errors using defined constructors from createError
  • Fix error names that are not identifiers
    • createError["I'mateapot"] is now createError.ImATeapot
  • Set a meaningful name property on constructed errors

2014-12-09 / 1.2.8

  • Fix stack trace from exported function
  • Remove arguments.callee usage

2014-10-14 / 1.2.7

  • Remove duplicate line

2014-10-02 / 1.2.6

  • Fix expose to be true for ClientError constructor

2014-09-28 / 1.2.5

  • deps: statuses@1

2014-09-21 / 1.2.4

  • Fix dependency version to work with old npms

2014-09-21 / 1.2.3

  • deps: statuses@~1.1.0

2014-09-21 / 1.2.2

  • Fix publish error

2014-09-21 / 1.2.1

  • Support Node.js 0.6
  • Use inherits instead of util

2014-09-09 / 1.2.0

  • Fix the way inheriting functions
  • Support expose being provided in properties argument

2014-09-08 / 1.1.0

  • Default status to 500
  • Support provided error to extend

2014-09-08 / 1.0.1

  • Fix accepting string message

2014-09-08 / 1.0.0

  • Initial release