http-errors vs http-status-codes vs boom vs http-status vs create-error
Error Handling Libraries in Node.js Comparison
1 Year
http-errorshttp-status-codesboomhttp-statuscreate-errorSimilar Packages:
What's Error Handling Libraries in Node.js?

These libraries provide various utilities for error handling in Node.js applications. They help standardize error responses, improve error management, and enhance the overall robustness of applications. Each library has its unique features and use cases, making them suitable for different scenarios in web development.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
http-errors60,831,8731,51518.8 kB123 years agoMIT
http-status-codes2,460,0241,061223 kB33a year agoMIT
boom1,641,8972,936-66 years agoBSD-3-Clause
http-status596,009472338 kB32 months agoBSD-3-Clause
create-error184,992103-511 years agoMIT
Feature Comparison: http-errors vs http-status-codes vs boom vs http-status vs create-error

Error Object Creation

  • http-errors:

    http-errors simplifies the creation of HTTP errors. You can create errors with specific status codes and messages, making it easy to handle errors in Express middleware and route handlers.

  • http-status-codes:

    Similar to http-status, http-status-codes provides a simple way to reference HTTP status codes. It does not create error objects but serves as a quick reference for developers.

  • boom:

    Boom provides a rich API for creating error objects with detailed information, including status codes, messages, and additional data. It allows you to generate errors that are structured and informative, making it easier to handle errors in a consistent manner.

  • http-status:

    http-status does not create error objects but provides a mapping of HTTP status codes to their descriptions. This helps in managing and referencing status codes throughout your application, improving code clarity.

  • create-error:

    create-error allows you to define custom error classes easily. You can create errors with specific messages and properties, which helps in categorizing and managing different types of errors in your application.

Integration with Express

  • http-errors:

    http-errors is designed specifically for use with Express. It allows you to create and send HTTP errors directly, making it a preferred choice for middleware and route error handling in Express applications.

  • http-status-codes:

    http-status-codes can be used in Express applications to reference status codes, but it does not provide direct error handling capabilities.

  • boom:

    Boom integrates seamlessly with Express, allowing you to return structured error responses directly from your route handlers. This makes it easy to manage errors in a consistent way across your API.

  • http-status:

    http-status is not specifically designed for error handling but can be used alongside other libraries to improve the readability of status codes in Express responses.

  • create-error:

    create-error can be used in Express applications to throw custom errors. However, it requires additional handling to convert these errors into HTTP responses, as it does not provide built-in integration with Express.

Customization

  • http-errors:

    http-errors offers limited customization options, primarily focusing on standard HTTP error responses. While you can set messages and status codes, it does not support extensive customization like Boom or create-error.

  • http-status-codes:

    http-status-codes is a simple reference library and does not offer customization capabilities for error handling.

  • boom:

    Boom allows for extensive customization of error responses, including the ability to add custom properties and metadata to the error objects. This is useful for providing additional context in error responses.

  • http-status:

    http-status does not provide customization options for error responses, as it is primarily a utility for referencing status codes.

  • create-error:

    create-error is highly customizable, allowing you to define specific properties and methods for your custom error classes. This flexibility makes it easy to tailor error handling to your application's needs.

Learning Curve

  • http-errors:

    http-errors is simple to use, especially for those familiar with Express. Its API is straightforward, making it easy to integrate into existing applications.

  • http-status-codes:

    http-status-codes is also easy to learn, providing a simple way to access HTTP status codes without additional complexity.

  • boom:

    Boom has a moderate learning curve due to its comprehensive API and features. Developers may need some time to fully understand how to leverage its capabilities effectively.

  • http-status:

    http-status has a very low learning curve, as it simply provides a mapping of status codes. Developers can quickly reference codes without needing to learn complex features.

  • create-error:

    create-error is easy to learn and use, making it a good choice for developers looking for a straightforward way to create custom errors without much overhead.

Community and Maintenance

  • http-errors:

    http-errors is widely used in the Express community and is actively maintained, ensuring compatibility with the latest versions of Express and Node.js.

  • http-status-codes:

    http-status-codes is also stable and well-maintained, but like http-status, it does not see frequent updates as it serves a specific purpose.

  • boom:

    Boom has a strong community and is actively maintained, ensuring that it stays up-to-date with best practices and new features in error handling.

  • http-status:

    http-status is a stable library with a solid community backing, but it is less frequently updated compared to others focused on error handling.

  • create-error:

    create-error is well-maintained and has a growing community, making it a reliable choice for custom error handling in Node.js applications.

How to Choose: http-errors vs http-status-codes vs boom vs http-status vs create-error
  • http-errors:

    Opt for http-errors if you want a straightforward way to create HTTP errors with appropriate status codes. This package is perfect for middleware in Express applications where you need to send error responses based on HTTP status codes.

  • http-status-codes:

    Choose http-status-codes for a lightweight and straightforward way to work with HTTP status codes. This package is ideal for projects where you want a simple reference for status codes without additional features.

  • boom:

    Choose Boom if you need a comprehensive error handling solution that provides detailed HTTP-friendly error objects, including status codes and payloads. It's ideal for APIs where you want to maintain a consistent error response format.

  • http-status:

    Use http-status if you need a simple way to manage HTTP status codes in your application. It provides a clear mapping of status codes to their descriptions, making your code more readable and maintainable.

  • create-error:

    Select create-error for a lightweight solution that allows you to create custom error classes easily. It's suitable for applications that require a simple way to define and throw errors without additional overhead.

README for 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