A library that provides extensible ES6 HTTP error classes.
npm install @ebec/http



A library that provides extensible ES6 HTTP error classes that define basic information about the error.
Table of Contents
- Installation
- Usage
- Classes
- Base
- Client
- Server
- Utils
- License
``bash`
npm install @ebec/http --save
The usage is pretty easy, just import one of the client or server error classes and use them
in throw- & catch- statements.
Basic
`typescript
import {
InternalServerError,
NotFoundError
} from "@ebec/http";
const clientError = new NotFoundError();
console.log(clientError.statusCode);
// 404
console.log(clientError.logMessage);
// false
console.log(clientError.code);
// NOT_FOUND
// ------------------------------------
const serverError = new InternalServerError({
logLevel: 'warning'
});
console.log(clientError.statusCode);
// 500
console.log(clientError.logMessage);
// true
console.log(clientError.code);
// INTERNAL_SERVER_ERROR
console.log(clientError.logLevel);
// warning
`
Another way to use the predefined error classes is to extend them,
with own options.
Extend
`typescript
import {
Options,
mergeOptions,
NotFoundError
} from "@ebec/http";
class UserNotFound extends NotFoundError {
constructor() {
super({
statusMessage: 'The user was not found.',
code: 'USER_NOT_FOUND'
});
}
}
`
The following HTTP classes are predefined:
- HTTPErrorClientError
- ServerError
-
- 400 BadRequestErrorUnauthorizedError
- 401 ForbiddenError
- 403 NotFoundError
- 404 MethodNotAllowedError
- 405 NotAcceptableError
- 406 ProxyAuthenticationRequiredError
- 407 RequestTimeoutError
- 408 ConflictError
- 409 GoneError
- 410 LengthRequiredError
- 411 PreconditionFailedError
- 412 RequestEntityTooLargeError
- 413 RequestUriTooLongError
- 414 UnsupportedMediaTypeError
- 415 RequestRangeNotSatisfiedError
- 416 ExpectationFailedError
- 417 ImATeapotError
- 418 EnhanceYourCalmError
- 420 UnprocessableEntityError
- 422 LockedError
- 423 FailedDependencyError
- 424 UnorderedCollectionError
- 424 UpgradeRequiredError
- 426 PreconditionRequiredError
- 428 TooManyRequestError
- 429 RequestHeaderFieldsTooLargeError
- 431 NoResponseError
- 444 RetryWithError
- 449 BlockedByWindowsParentError
- 450 ClientClosedRequestError
- 499
- 500 InternalServerErrorNotImplementedError
- 501 BadGatewayError
- 502 ServiceUnavailableError
- 503 GatewayTimeoutError
- 504 HTTPVersionNotSupportedError
- 505 VariantAlsoNegotiates
- 506 InsufficientStorageError
- 507 LoopDetectedError
- 508 BandwidthLimitExceededError
- 509 NotExtendedError
- 510 NetworkAuthenticationRequiredError
- 511
This method determines whether it is a client or server error.
`typescript
import { isHTTPError, NotFoundError } from "@ebec/http";
const error = new NotFoundError();
console.log(isHTTPError(error));
// true
`
This method determines whether it is a client error.
`typescript
import { isClientError, NotFoundError } from "@ebec/http";
const error = new NotFoundError();
console.log(isClientError(error));
// true
`
This method determines whether it is a server error.
`typescript
import { isServerError, NotFoundError } from "@ebec/http";
const error = new NotFoundError();
console.log(isServerError(error));
// false
``
Made with 💚
Published under MIT License.