Easy HTTP Errors
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
-
TypeError: (0, _easyHttpErrors.BadRequestError) is not a constructor
cause This usually indicates an issue with CommonJS interoperability when using `import` statements, or incorrect named import for a CommonJS default export.fixTry using the CommonJS `require` syntax: `const { BadRequestError } = require('easy-http-errors');` if your environment is CommonJS. If you are in an ES Module environment, ensure your build tool or Node.js version properly handles CommonJS named exports. -
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax in a file that Node.js or your build system treats as a CommonJS module.fixRename your file to `.mjs` or add `"type": "module"` to your `package.json` if using Node.js. Alternatively, convert your imports to CommonJS `require` statements: `const { BadRequestError } = require('easy-http-errors');`.
Warnings
- breaking Version 2.0.0 dropped support for Node.js 4. Applications targeting older Node.js versions must remain on v1.x or upgrade their Node.js environment to version 6 or higher.
- gotcha The project appears to be abandoned or in deep maintenance, with the last major release (v2.0.0) dating back to 2017. Users should not expect new features, bug fixes, or security updates. This might pose a long-term risk for applications depending on active maintenance.
- gotcha While the README shows ES6 import syntax, given the release date (2017) and common practices of the time, the package likely primarily uses CommonJS `module.exports`. If encountering issues with `import` syntax, ensure your environment is set up for ES Modules or use `require()`.
Install
-
npm install easy-http-errors -
yarn add easy-http-errors -
pnpm add easy-http-errors
Imports
- BadRequestError
const BadRequestError = require('easy-http-errors').BadRequestError;import { BadRequestError } from 'easy-http-errors'; - NotFoundError
import NotFoundError from 'easy-http-errors';
import { NotFoundError } from 'easy-http-errors'; - InternalServerError
const InternalServerError = require('easy-http-errors');const { InternalServerError } = require('easy-http-errors');
Quickstart
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