http-errors vs http-status-codes vs boom vs http-status vs create-error
エラーハンドリングライブラリ
http-errorshttp-status-codesboomhttp-statuscreate-error類似パッケージ:
エラーハンドリングライブラリ

エラーハンドリングライブラリは、Node.jsアプリケーションにおいてエラーを管理し、適切なレスポンスを生成するためのツールです。これらのライブラリは、エラーメッセージの標準化、HTTPステータスコードの管理、エラーハンドリングの簡素化を提供し、開発者がエラー処理を効率的に行えるようにします。

npmのダウンロードトレンド
3 年
GitHub Starsランキング
統計詳細
パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
http-errors82,262,1741,54719.1 kB1511日前MIT
http-status-codes5,234,0781,102223 kB322年前MIT
boom3,302,6182,931-77年前BSD-3-Clause
http-status784,549485338 kB41年前BSD-3-Clause
create-error268,316103-512年前MIT
機能比較: http-errors vs http-status-codes vs boom vs http-status vs create-error

エラーレスポンスの生成

  • http-errors:

    Http-errorsは、HTTPステータスコードに基づいたエラーを生成するためのシンプルなAPIを提供します。エラーのメッセージをカスタマイズすることもでき、HTTPエラーの管理が容易です。

  • http-status-codes:

    Http-status-codesは、HTTPステータスコードを定数として提供し、エラー処理をより明確にします。これにより、コードの可読性が向上し、エラーの管理が容易になります。

  • boom:

    Boomは、HTTPエラーを簡単に生成するための便利なメソッドを提供します。特に、エラーの詳細情報を含めることができ、開発者がエラーを追跡しやすくなります。

  • http-status:

    Http-statusは、HTTPステータスコードを定義し、可読性の高いコードを書くのに役立ちます。これにより、エラーハンドリングがより直感的になります。

  • create-error:

    Create-errorは、カスタムエラーを作成するためのシンプルなインターフェースを提供します。エラーのメッセージやスタックトレースを簡単に設定でき、柔軟なエラーハンドリングが可能です。

カスタマイズ性

  • http-errors:

    Http-errorsは、エラーメッセージをカスタマイズする機能を提供しており、特定の状況に応じたエラーメッセージを設定できます。

  • http-status-codes:

    Http-status-codesは、定数としてHTTPステータスコードを提供するため、エラー処理の際に一貫性を持たせることができます。

  • boom:

    Boomは、エラーメッセージやHTTPステータスコードを簡単にカスタマイズできるため、特定のニーズに合わせたエラーハンドリングが可能です。

  • http-status:

    Http-statusは、HTTPステータスコードを簡単に取得できるため、エラー処理の際に柔軟に対応できます。

  • create-error:

    Create-errorは、エラーオブジェクトを自由にカスタマイズできるため、特定のビジネスロジックに基づいたエラー処理が容易です。

学習曲線

  • http-errors:

    Http-errorsは、HTTPエラーに特化しているため、特定の用途に対して学習が容易です。HTTPに関する知識があればすぐに使えます。

  • http-status-codes:

    Http-status-codesは、定数として提供されるため、学習曲線は非常に緩やかで、すぐに使い始めることができます。

  • boom:

    Boomは、直感的なAPIを提供しているため、比較的学習しやすいです。特に、エラーハンドリングに関する知識があればすぐに使い始めることができます。

  • http-status:

    Http-statusは、HTTPステータスコードの理解を深めるのに役立ち、学習曲線は比較的緩やかです。

  • create-error:

    Create-errorは、シンプルなインターフェースを持っているため、初心者でも簡単に理解できます。エラー処理の基本を学ぶのに適しています。

メンテナンス性

  • http-errors:

    Http-errorsは、HTTPエラーに特化しているため、エラー処理のメンテナンスが容易です。特に、HTTPステータスコードに基づいたエラー管理が簡単です。

  • http-status-codes:

    Http-status-codesは、定数として提供されるため、メンテナンスが容易で、エラー処理の一貫性を保つことができます。

  • boom:

    Boomは、エラーハンドリングのための標準化された方法を提供するため、メンテナンスが容易です。エラー処理の一貫性を保つことができます。

  • http-status:

    Http-statusは、HTTPステータスコードを一元管理できるため、メンテナンス性が高く、コードの可読性が向上します。

  • create-error:

    Create-errorは、カスタムエラーを簡単に管理できるため、メンテナンスが容易で、特定のビジネスロジックに基づいたエラー処理が可能です。

選び方: http-errors vs http-status-codes vs boom vs http-status vs create-error
  • http-errors:

    Http-errorsは、HTTPエラーを簡単に生成するためのライブラリです。特に、HTTPステータスコードに基づいたエラーを扱う際に便利で、エラーメッセージを簡単にカスタマイズできます。

  • http-status-codes:

    Http-status-codesは、HTTPステータスコードを定数として使用したい場合に適しています。これにより、コードの可読性が向上し、エラー処理がより直感的になります。

  • boom:

    Boomは、エラーレスポンスを簡単に生成したい場合に選択してください。特に、HTTPエラーの詳細な情報を提供し、エラーハンドリングのための便利なメソッドを多く持っています。

  • http-status:

    Http-statusは、HTTPステータスコードを簡単に扱いたい場合に選択してください。ステータスコードの定義を提供し、可読性の高いコードを書くのに役立ちます。

  • create-error:

    Create-errorは、シンプルでカスタマイズ可能なエラーオブジェクトを作成したい場合に適しています。エラーのスタックトレースを保持し、エラーの種類に応じたカスタムエラーを簡単に作成できます。

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

License

MIT