Skip to content

nfp-projects/http-errors-lite

 
 

Repository files navigation

http-errors-lite

npm version NPM Downloads Node.js Version Build Status

Create HTTP errors for Express, Koa, Connect, etc. with ease. This is a drop-in replacement for the normal http-errors only with zero dependancies and 50% less memory usage (give or take).

Breaking change to http-errors

Normal http-errors doesn't care about the order of arguments but would throw a deprecated error if status was put after the message:

var createError = require('http-errors')
// The following would throw deprecated
createError('Not found', 404) // Should be: createError(404, 'Not Found')

The http-errors-lite takes on this more strictly and cares about ordering more.

var createError = require('http-errors-lite')
// All of the following will work as expected:
createError('Something happened')
createError('This happened', { props: 42 })
createError(422, { id: 1 })
createError(422, 'That is not allowed')
createError({ id: 3242, test: 'true' })
// The following however will not work as expected:
createError('Not found', 404) // Due to status not being in front, will default to 500

As long as you put status in front of message or whatever, you will be fine :)

Install

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

$ npm install http-errors-lite

Example

var createError = require('http-errors-lite')
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

License

MIT

About

Create HTTP Errors, now with zero dependancies and less memory usage

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%