🚀 DAY 4 OF LAUNCH WEEK: Introducing GitHub Actions Scanning Support.Learn more →
Socket
Book a DemoInstallSign in
Socket

@jmondi/http-err

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jmondi/http-err

HttpErrors with status codes and context

latest
npmnpm
Version
0.2.1
Version published
Maintainers
1
Created
Source

@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);

FAQs

Package last updated on 09 Sep 2022

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