@financial-times/ip-custom-errors
v1.0.1
Published
Generates custom Http errors
Downloads
111
Maintainers
Keywords
Readme
ip-custom-errors
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