Easy HTTP Errors

2.0.0 · abandoned · verified Wed Apr 22

Easy HTTP Errors (easy-http-errors) is a JavaScript library designed to provide a straightforward way to handle and throw common HTTP errors within applications. It offers a set of predefined error classes, each corresponding to a standard HTTP status code (e.g., `BadRequestError` for 400, `NotFoundError` for 404, `InternalServerError` for 500). Developers can instantiate these error classes, optionally providing a custom message and additional properties, simplifying error propagation and handling in web contexts. The current stable version is 2.0.0, released in 2017, which notably dropped support for Node.js 4. Given the lack of updates since then, the project appears to be unmaintained, suggesting a slow to non-existent release cadence. Its primary differentiator lies in providing a convenient, structured hierarchy for standard HTTP exceptions, abstracting the need for manual status code assignment and message formatting.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing and throwing various HTTP error classes with custom messages, then catching and handling them based on their type.

import { BadRequestError, NotFoundError } from 'easy-http-errors';

function handleRequest(userId, itemId) {
  try {
    if (!userId) {
      throw new BadRequestError('User ID is required.');
    }
    // Simulate a database lookup for an item
    const item = { id: itemId, name: 'Example Item' }; // Replace with actual lookup

    if (!item || item.id !== itemId) {
      throw new NotFoundError(`Item with ID ${itemId} not found.`);
    }

    console.log(`Processing item ${item.name} for user ${userId}`);
    // ... further processing

  } catch (error) {
    if (error instanceof BadRequestError || error instanceof NotFoundError) {
      console.error(`HTTP Error (${error.statusCode}): ${error.message}`);
      // In a real application, send this error back to the client
      // e.g., res.status(error.statusCode).json({ message: error.message, details: error.properties });
    } else {
      console.error('An unexpected error occurred:', error.message);
      // Handle other types of errors, potentially a 500 Internal Server Error
    }
  }
}

handleRequest('user123', 'item456');
handleRequest('user123', 'item999'); // Simulate not found
handleRequest(null, 'item456'); // Simulate bad request

view raw JSON →