Socket
Socket
Sign inDemoInstall

@ezee/http-errors

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @ezee/http-errors

A simple package to create HTTP errors for Typescript projects


Version published
Weekly downloads
41
decreased by-47.44%
Maintainers
1
Install size
5.80 kB
Created
Weekly downloads
 

Readme

Source

@ezee/http-errors

Create beautiful and consistent HTTP errors with ease.

Install

With npm

npm install @ezee/http-errors

With yarn

yarn add @ezee/http-errors

With pnpm

pnpm add @ezee/http-errors

Usage

import httpErrors from '@ezee/http-errors';

const error = new httpErrors.Notfound({
  message: 'User not found',
  code: 'USER_NOT_FOUND',
  meta: {
    userId: 123,
  },
});

How to instanciate an error

const error = new httpErrors.<ErrorName>(options);

Options

  • message - The error message.
  • code - The error code.
  • meta - Additional information about the error. In object format. (optional)

Available Errors

  • BadRequest
  • Unauthorized
  • Forbidden
  • Notfound
  • MethodNotAllowed
  • Conflict
  • InternalServerError
  • NotImplemented
  • ServiceUnavailable
  • GatewayTimeout
  • PaymentRequired

Examples

With express

import express from 'express';
import httpErrors from '@ezee/http-errors';

const app = express();

app.get('/user/:id', (req, res, next) => {
  const { id } = req.params;

  if (id !== '123') {
    return next(
      new httpErrors.Notfound({
        message: 'User not found',
        code: 'USER_NOT_FOUND',
        meta: {
          userId: id,
        },
      })
    );
  }

  res.json({ id, name: 'John Doe' });
});

With koa

import Koa from 'koa';
import httpErrors from '@ezee/http-errors';

const app = new Koa();

app.use(async (ctx, next) => {
  const { id } = ctx.params;

  if (id !== '123') {
    ctx.throw(
      new httpErrors.Notfound({
        message: 'User not found',
        code: 'USER_NOT_FOUND',
        meta: {
          userId: id,
        },
      })
    );
  }

  ctx.body = { id, name: 'John Doe' };
});

Keywords

FAQs

Last updated on 07 Apr 2024

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