Socket
Socket
Sign inDemoInstall

throw-http-errors

Package Overview
Dependencies
0
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    throw-http-errors

Custom errors for HTTP status codes.


Version published
Maintainers
1
Install size
25.3 kB
Created

Readme

Source

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:

  • status[required]: The HTTP Status number of this error.

  • name[optional]: A unique identifier of this error.

  • message[optional]: Message of this error.

  • code[optional]: 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'));
});

Keywords

FAQs

Last updated on 18 Jan 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc