4.0.1 • Published 3 years ago

throw-http-errors v4.0.1

Weekly downloads
100
License
ISC
Repository
github
Last release
3 years ago

throw-http-errors

Custom errors for HTTP status codes. For the old js version look at the branch javascript

Build Status Coverage Status

Usage

First, install throw-http-errors as a dependency:

npm install --save throw-http-errors

HTTP Errors

  const errors = require('throw-http-errors');

Parameters:

400 BadRequest

  new errors.BadRequest('Name required in body', 190);
            OR
  new errors[400](msg, code);

401 Unauthorized

  new errors.Unauthorized(msg, code);
            OR
  new errors[401](msg, code);

402 PaymentRequired

  new errors.PaymentRequired(msg, code);
            OR
  new errors[402](msg, code);

403 Forbidden

  new errors.Forbidden(msg, code);
            OR
  new errors[403](msg, code);

404 NotFound

  new errors.NotFound(msg, code);
            OR
  new errors[404](msg, code);

405 MethodNotAllowed

  new errors.MethodNotAllowed(msg, code);
            OR
  new errors[405](msg, code);

500 InternalServerError

  new errors.InternalServerError(msg, code);
            OR
  new errors[500](msg, code);

Supported HTTP ERRORS

HTTPError NameHTTP Status
BadRequest400
Unauthorized401
PaymentRequired402
Forbidden403
NotFound404
MethodNotAllowed405
NotAcceptable406
ProxyAuthenticationRequired407
RequestTimeout408
Conflict409
Gone410
LengthRequired411
PreconditionFailed412
PayloadTooLarge413
URITooLong414
UnsupportedMediaType415
RangeNotSatisfiable416
ExpectationFailed417
MisdirectedRequest421
UnprocessableEntity422
Locked423
FailedDependency424
UnorderedCollection425
UpgradeRequired426
PreconditionRequired428
TooManyRequests429
UnavailableForLegalReasons431
RequestHeaderFieldsTooLarge451
InternalServerError500
NotImplemented501
BadGateway502
ServiceUnavailable503
GatewayTimeout504
HTTPVersionNotSupported505
VariantAlsoNegotiates506
InsufficientStorage507
LoopDetected508
NotExtended510
NetworkAuthenticationRequired511

Custom Errors

  new errors.CreateCustomError(status, name, message, code);

Parameters:

  • statusrequired: The HTTP Status number of this error.

  • nameoptional: A unique identifier of this error.

  • messageoptional: Message of this error.

  • codeoptional: A unique code of this error.

Example

In order to use it you need to require the module and then just throw a custom error:

const express = require('express');
const app = express();
const errors = require('throw-http-errors');

app.get('/user/:id', (req, res, next) => {
  next(new errors.NotFound('User not found', 'USER_NOT_FOUND'));
});
3.2.0

3 years ago

3.0.1

3 years ago

4.0.1

3 years ago

2.0.0

6 years ago

1.0.17

7 years ago

1.0.15

7 years ago

1.0.13

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago