Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@kamalyb/errors

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kamalyb/errors

errors

  • 0.1.0
  • unpublished
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by66.67%
Maintainers
1
Weekly downloads
 
Created
Source

errors

solely for personal need instead of repeating in every project

example

import express, { Express } from "express";
import {
  BadRequestError,
  ForbiddenError,
  InternalServerError,
  NotAuthorizedError,
  NotFoundError,
  RateLimitError,
  ValidationError,
  ValidatorValidationError,
  CustomError,
} from "@kamalyb/errors";

type ErrorType =
  | "BadRequest"
  | "Forbidden"
  | "InternalServer"
  | "NotAuthorized"
  | "NotFound"
  | "RateLimit"
  | "Validation"
  | "ValidatorValidation";

const app: Express = express();

app.get("/throw", (req, res) => {
  const type: ErrorType = req.body.type;

  switch (type) {
    case "BadRequest":
      /**
       * @param string | { message: string, field?: string } | string[] | { message: string, field?: string }[]
       * @required true
       * @status 400
       */
      throw new BadRequestError({ message: "invalid data" });
    case "Forbidden":
      /**
       * @param string
       * @required false
       * @default "thou shalt not"
       * @status 403
       */
      throw new ForbiddenError();
    case "InternalServer":
      /**
       * @status 500
       */
      throw new InternalServerError();
    case "NotAuthorized":
      /**
       * @param string
       * @required false
       * @default "you shall not pass"
       * @status 500
       */
      throw new NotAuthorizedError();
    case "NotFound":
      /**
       * @param string | { message: string, field?: string } | string[] | { message: string, field?: string }[]
       * @required true
       * @status 404
       */
      throw new NotFoundError();
    case "RateLimit":
      /**
       * @param string
       * @required false
       * @default "too many requests. please try again later"
       * @status 429
       */
      throw new RateLimitError();
    case "Validation":
      /**
       * @param string | { message: string, field?: string } | string[] | { message: string, field?: string }[]
       * @required true
       * @status 422
       */
      throw new ValidationError();
    case "ValidatorValidation":
      /**
       * @param validationResult(req).array()
       * @required true
       * @status 422
       */
      throw new ValidatorValidationError();
  }
});

app.use((error: Error, req: Request, res: Response, next: NextFunction) => {
  if (error instanceof CustomError) {
    return res.status(error.status).send({ errors: error.serialize() });
    // sends { errors: [{ message: "i am error message", field: "i am error field or undefined"}] }
  }

  res.status(500).send({
    errors: [
      {
        message: "internal server error",
      },
    ],
  });
});

app.listen(80);

FAQs

Package last updated on 25 Nov 2021

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc