Tired of messy error handling, inconsistent API responses, and fragile Node.js applications? Error-Cure is your one-stop solution for building robust, production-ready, and developer-friendly APIs with Express.
Error-Cure provides a comprehensive suite of tools to streamline your error handling workflow, from custom error classes to a powerful global middleware. It's designed to be lightweight, easy to use, and highly configurable, allowing you to focus on what matters most: building great applications.
| Feature | Benefit |
|---|---|
| Centralized Handling | Manage all errors in one place, ensuring consistent and predictable API responses. |
| Improved Debugging | Differentiate between operational errors (user-facing) and programming errors (internal bugs). |
| Enhanced User Experience | Provide clear, meaningful error messages to your users without exposing sensitive stack traces. |
| Increased Stability | Gracefully handle unhandled promise rejections and uncaught exceptions to prevent crashes. |
| Accelerated Development | Reduce boilerplate code and focus on your core application logic. |
| TypeScript & JS Support | Seamlessly integrate with both TypeScript and JavaScript projects (CommonJS & ESM). |
- πͺ Custom Error Classes: A set of pre-built, extensible error classes for common scenarios:
AppError: The base class for all operational errors.ValidationError: For input validation failures (400).AuthError: For authentication/authorization issues (401).NotFoundError: For missing resources (404).DatabaseError: For database-related failures (500).
- π‘οΈ Global Error Middleware: A powerful Express middleware that catches all errors and sends beautiful, environment-aware responses.
- π Error Logging: A simple yet effective utility to log errors to a file for easy debugging and monitoring.
- π¨ Unhandled Rejection/Exception Handling: Automatically catches and logs unhandled promise rejections and uncaught exceptions.
- β TypeScript Ready: Written entirely in TypeScript with type definitions included.
- π¦ Dual Module Support: Supports both CommonJS (
require) and ES Modules (import).
# Using npm
npm install error-cure
# Using yarn
yarn add error-cureerror-cure/
βββ dist/
β βββ cjs/ # CommonJS output
β βββ esm/ # ES Module output
βββ src/
β βββ index.ts # Main entry point
β βββ errors/
β β βββ AppError.ts
β β βββ AuthError.ts
β β βββ DataBaseError.ts
β β βββ NotFoundError.ts
β β βββ ValidationError.ts
β βββ middleware/
β β βββ globalErrorHandler.ts
β βββ utils/
β βββ handleRejections.ts
β βββ logError.ts
βββ tests/
βββ ...
Error-Cure is designed to be intuitive and easy to integrate. Hereβs how you can use it in your projects:
// app.ts
import express from 'express';
import { AppError, globalErrorHandler, handleUnhandledRejections } from 'error-cure';
// Handle unhandled rejections and exceptions
handleUnhandledRejections();
const app = express();
app.get('/users/:id', (req, res, next) => {
if (req.params.id === '0') {
return next(new AppError('Invalid user ID.', 400));
}
res.json({ id: req.params.id, name: 'John Doe' });
});
// Handle 404 errors
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
});
// Global error handling middleware (must be last)
app.use(globalErrorHandler);
app.listen(3000, () => console.log('Server running on port 3000'));// server.js
const express = require('express');
const { AppError, globalErrorHandler, handleUnhandledRejections } = require('error-cure');
// Handle unhandled rejections and exceptions
handleUnhandledRejections();
const app = express();
app.get('/users/:id', (req, res, next) => {
if (req.params.id === '0') {
return next(new AppError('Invalid user ID.', 400));
}
res.json({ id: req.params.id, name: 'John Doe' });
});
// Handle 404 errors
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
});
// Global error handling middleware (must be last)
app.use(globalErrorHandler);
app.listen(3000, () => console.log('Server running on port 3000'));The globalErrorHandler automatically adapts its response based on the NODE_ENV environment variable:
development: Provides detailed error information, including stack traces.production: Sends minimal, user-friendly error messages.
Always set NODE_ENV=production in your production environment.
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature). - Make your changes and add tests.
- Ensure all tests pass (
npm test). - Commit your changes (
git commit -m 'feat: Add some feature'). - Push to the branch (
git push origin feature/your-feature). - Open a Pull Request.
This project is licensed under the MIT License. See the LICENSE.txt file for details.
error-handling, express, node.js, middleware, error-management, custom-errors, api-errors, unhandled-rejection, uncaught-exception, logging, typescript, javascript, commonjs, esm, express-middleware, nodejs, error-logger, production-ready, developer-tools, api, backend, rest-api, error-response, json-errors