1.0.1 • Published 6 years ago

@financial-times/ip-custom-errors v1.0.1

Weekly downloads
266
License
MIT
Repository
github
Last release
6 years ago

ip-custom-errors

npm version CircleCI

A module that generates custom HTTP errors

setup

Install via npm:

  npm i -S @financial-times/ip-custom-errors

Usage

The module exposes three methods to generate the custom HTTP errors. You can use the one that fits your needs as follows:

Using the custom HttpError constructor to create a custom error message, for example:

const { HTTPError } = require('@financial-times/ip-custom-errors');
try {
  ...
  throw new HTTPError(401,'Unauthorized','You are not allowed to access this resource')
} catch (err) {
  console.error(err)
}

Using the custom HTTPStatusError method with status code and custom message, for example:

const { HTTPStatusErrors } = require('@financial-times/ip-custom-errors');
try {
  ...
  throw HTTPStatusErrors['400']('You are not allowed to access this resource')
} catch (err) {
  console.error(err)
}

Using the name of the custom error by name

const { NotFound } = require('@financial-times/ip-custom-errors');
try {
  ...
  throw NotFound("The resource you requested can't be found");
} catch (err) {
  console.error(err)
}

List of error names to use

BadRequestUnsupportedMediaTypeNotImplemented
UnauthorizedRangeNotSatisfiableBadGateway
PaymentRequiredExpectationHttpVersionNotSupported
ForbiddenMisdirectedRequestVariantAlsoNegotiates
NotFoundUnprocessableEntityInsufficientStorage
MethodNotAllowedLockedLoopDetected
NotAcceptableUnorderedCollectionNotExtended
ProxyAuthenticationRequiredUpgradeRequiredNetworkAuthenticationRequired
RequestTimeoutPreconditionRequiredServiceUnavailable
ConflictTooManyRequestsGatewayTimeout
GoneUnavailableForLegalReasonsBandwidthLimitExceeded
LengthRequiredInternalServerError
PreconditionFailedImATeapot
PayloadTooLargeFailedDependency
UriTooLongRequestHeaderFieldsTooLarge

Licence

MIT