Throwable HTTP Errors

2.0.3 · active · verified Wed Apr 22

Throwable HTTP Errors is a Node.js library designed to provide a comprehensive set of strongly-typed HTTP exception classes (e.g., BadRequest, NotFound, InternalServerError) for use in API development. The library, currently at stable version 2.0.3, aims to streamline error handling by integrating naturally with modern JavaScript's `async/await` syntax and `try/catch` blocks. It differentiates itself from older patterns that relied solely on `next(error)` callbacks by promoting the throwing of specific HTTP-related errors, which can then be caught and handled with standard error flow control. This approach enhances code readability and consistency in handling HTTP-specific error conditions across an application. While a specific release cadence isn't published, the current version was released over a year ago, indicating a mature and relatively stable project. The package also ships with TypeScript type definitions, making it suitable for modern TypeScript projects.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating and handling throwable HTTP errors within an Express.js API using `async/await` and a global error middleware, showcasing `BadRequest` and `NotFound` errors.

import express from 'express';
import { BadRequest, NotFound, InternalServerError } from 'throwable-http-errors';

const app = express();
app.use(express.json());

interface Pet { id: string; name: string; type: string; }

// Dummy database and controller
const pets: Pet[] = [{ id: '1', name: 'Buddy', type: 'Dog' }];
const PetsController = {
  create: async (data: any): Promise<Pet> => {
    if (!data || !data.name || !data.type) {
      throw new BadRequest('Pet name and type are required.');
    }
    const newPet: Pet = { id: String(pets.length + 1), ...data };
    pets.push(newPet);
    return newPet;
  },
  getById: async (id: string): Promise<Pet> => {
    const pet = pets.find(p => p.id === id);
    if (!pet) {
      throw new NotFound(`Pet with ID ${id} not found.`);
    }
    return pet;
  }
};

app.post('/pets', async (req, res, next) => {
  try {
    const pet = await PetsController.create(req.body);
    res.status(201).send({ status: true, data: pet });
  } catch (e) {
    next(e); // Pass error to the global error handler
  }
});

app.get('/pets/:id', async (req, res, next) => {
  try {
    const pet = await PetsController.getById(req.params.id);
    res.status(200).send({ status: true, data: pet });
  } catch (e) {
    next(e);
  }
});

// Global error handling middleware
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
  // All throwable-http-errors instances have a 'status' property
  if (err.status) {
    return res.status(err.status).json({ status: false, message: err.message });
  } else {
    console.error(err);
    return res.status(500).json({ status: false, message: 'Internal Server Error' });
  }
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
  console.log('Try: POST /pets with { "name": "Fluffy", "type": "Cat" }');
  console.log('Then: GET /pets/1');
});

view raw JSON →