@financial-times/ip-custom-errors

1.0.1 • Public • Published

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

BadRequest UnsupportedMediaType NotImplemented
Unauthorized RangeNotSatisfiable BadGateway
PaymentRequired Expectation HttpVersionNotSupported
Forbidden MisdirectedRequest VariantAlsoNegotiates
NotFound UnprocessableEntity InsufficientStorage
MethodNotAllowed Locked LoopDetected
NotAcceptable UnorderedCollection NotExtended
ProxyAuthenticationRequired UpgradeRequired NetworkAuthenticationRequired
RequestTimeout PreconditionRequired ServiceUnavailable
Conflict TooManyRequests GatewayTimeout
Gone UnavailableForLegalReasons BandwidthLimitExceeded
LengthRequired InternalServerError
PreconditionFailed ImATeapot
PayloadTooLarge FailedDependency
UriTooLong RequestHeaderFieldsTooLarge

Licence

MIT

Readme

Keywords

none

Package Sidebar

Install

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

Weekly Downloads

8

Version

1.0.1

License

MIT

Unpacked Size

8.11 kB

Total Files

4

Last publish

Collaborators

  • robertboulton
  • seraph2000
  • hamza.samih
  • notlee
  • emmalewis
  • aendra
  • the-ft
  • rowanmanning
  • chee
  • alexwilson