1.0.2 • Published 6 years ago

@lleon/http-errors v1.0.2

Weekly downloads
-
License
MIT
Repository
bitbucket
Last release
6 years ago

HTTP Errors

Install

npm install @lleon/http-errors

Example

Test instance

import { HttpError, NotFound } from '@lleon/http-errors';

const error = new NotFound()

console.log(error instanceof Error)     // true
console.log(error instanceof HttpError) // true
console.log(error instanceof NotFound)  // true

With default options

import { NotFound } from '@lleon/http-errors';

const error = new NotFound();

console.log(error.statusCode); // 404
console.log(error.message);    // "Not Found"
console.log(error.toJSON());   // { "statusCode": 404, "message": "Not Found", "timestamp": 1512682132993, "metadata": {} }

With custom error message

import { NotFound } from '@lleon/http-errors';

const error = new NotFound('User not found');

console.log(error.statusCode); // 404
console.log(error.message);    // "User not found"
console.log(error.toJSON());   // { "statusCode": 404, "message": "User not Found", "timestamp": 1512682132993, "metadata": {} }

With custom metadata

import { NotFound } from '@lleon/http-errors';

const error = new NotFound({ id: 1 });

console.log(error.statusCode); // 404
console.log(error.message);    // "User not found"
console.log(error.toJSON());   // { "statusCode": 404, "message": "Not Found", "timestamp": 1512682132993, "metadata": { "id": 1 } }

With custom message and custom metadata

import { NotFound } from '@lleon/http-errors';

const error = new NotFound('User not found', { id: 1 });

console.log(error.statusCode); // 404
console.log(error.message); // "User not found"
console.log(error.toJSON()); // { "statusCode": 404, "message": "User not Found", "timestamp": 1512682132993, "metadata": { "id": 1 } }

Errors

ErrorCodeMessage
BadRequest400Bad Request
Unauthorized401Unauthorized
PaymentRequired402Payment Required
Forbidden403Forbidden
NotFound404Not Found
MethodNotAllowed405Method Not Allowed
NotAcceptable406Not Acceptable
ProxyAuthenticationRequired407Proxy Authentication Required
RequestTimeout408Request Timeout
Conflict409Conflict
Gone410Gone
LengthRequired411Length Required
PreconditionFailed412Precondition Failed
PayloadTooLarge413Payload Too Large
RequestUriTooLong414Request-URI Too Long
UnsupportedMediaType415Unsupported Media Type
RequestedRangeNotSatisfiable416Requested Range Not Satisfiable
ExpectationFailed417Expectation Failed
ImATeapot418I'm a teapot
MisdirectedRequest421Misdirected Request
UnprocessableEntity422Unprocessable Entity
Locked423Locked
FailedDependency424Failed Dependency
UpgradeRequired426Upgrade Required
PreconditionRequired428Precondition Required
TooManyRequests429Too Many Requests
RequestHeaderFieldsTooLarge431Request Header Fields Too Large
ConnectionClosedWithoutResponse444Connection Closed Without Response
UnavailableForLegalReasons451Unavailable For Legal Reasons
ClientClosedRequest499Client Closed Request
InternalServerError500Internal Server Error
NotImplemented501Not Implemented
BadGateway502Bad Gateway
ServiceUnavailable503Service Unavailable
GatewayTimeout504Gateway Timeout
HttpVersionNotSupported505HTTP Version Not Supported
VariantAlsoNegotiates506Variant Also Negotiates
InsufficientStorage507Insufficient Storage
LoopDetected508Loop Detected
NotExtended510Not Extended
NetworkAuthenticationRequired511Network Authentication Required
NetworkConnectTimeoutError599Network Connect Timeout Error
1.0.2

6 years ago

1.0.1

6 years ago