Giter VIP home page Giter VIP logo

http-error-classes's Introduction

HTTP error classes

Node.js CI

This package provides error classes for every HTTP 4xx and 5xx error code. This makes it easy to throw HTTP errors in your application without having to build the classes yourself for that, just a small win.

You can also check if an error is a HttpError by checking if it's an instance of the HttpError class from this package, all errors here extend that class.

The error classes allow you to provide a message and a context object, status codes are built into the errors. You can also just use the HttpError class to provide your own status code.

Example

Throwing an error with some context

Here we have a function to calculate the Fibonacci value at n steps. If the n we receive is not a number then we'll throw a BadRequestError and give the n we received as context.

const { BadRequestError } = require("http-error-classes");

const fibonacci = (n) => {
  if (typeof n !== "number") {
    throw new BadRequestError("n must be a number!", { n });
  }
  // Calculate fibonacci
};

Filtering for HttpErrors

Here we have an express error middleware that handles how errors get returned to the client. We don't want to expose errors that are not HttpErrors, so we mask them with a generic InternalServerError. It's also important to explicitly map the error to the returned object because HttpErrors, like regular Errors, contain the stack trace which should not be exposed.

const { HttpError, InternalServerError } = require("http-error-classes");

const errorHandlerMiddleware = (err, req, res, next) => {
  const error = err instanceof HttpError ? err : new InternalServerError();
  res.status(error.status).send({
    message: error.message,
    context: error.context,
  });
};

API

HttpError

declare class HttpError<ContextType> extends Error {
  readonly status: number;
  readonly message: string;
  readonly context?: ContextType | undefined;
  readonly name: string;
  readonly statusCode: number;

  constructor(
    status: number,
    message: string,
    context?: ContextType | undefined = undefined
  );
}

Any other error, for example BadRequestError

declare class BadRequestError<ContextType> extends HttpError<ContextType> {
  readonly message: string;
  readonly context?: ContextType | undefined;
  readonly name = "BadRequestError";

  constructor(
    message?: string = "Bad Request",
    context?: ContextType | undefined = undefined
  );
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.