0.2.1 • Published 3 years ago

@jmondi/http-err v0.2.1

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

@jmondi/http-err

Common http errors with status codes.

Install

pnpm add @jmondi/http-err

Usage

import { NotFoundError } from "@jmondi/http-err";

const id = "non-existant-id";

try {
  database.users.findBy(id);
} catch (e) {
  throw new NotFoundError(`[${id}] not found`);
}
import express from "express";
import { HttpError } from "@jmondi/http-err";

async function handleErrors(err, req, res, next) {
  if (err instanceof InternalServerError) {
    // Do something specific for InternalServerErrors
    // like logging to an exception handling service
  } else if (err instanceof HttpError) {
    return res.status(err.status).json({
      message: err.message,
      context: JSON.stringify(err.context),
    });
  }
  next(err);
}

app = express();
app.get("/api", apiRoutes);
app.use(handleErrors);