Error-Cure is a robust library for handling errors in Node.js and Express applications. It simplifies error management by providing custom error classes, middleware for centralized error handling, and utilities for logging errors and managing unhandled exceptions.
- Custom Error Classes: Includes
AppError
,ValidationError
,AuthError
, and more to cover diverse use cases. - Global Error Handler Middleware: Simplifies error handling in Express.js applications.
- Error Logging Utilities: Write errors to log files for better debugging and analysis.
- Unhandled Exception Management: Tools to handle unhandled promise rejections and uncaught exceptions.
- Lightweight & Extendable: Easy to integrate and customize for your application.
To get started, install the package using npm or yarn:
npm install error-cure
or
yarn add error-cure
Error-Cure includes several custom error classes that extend a base AppError
class. These can classify and handle different types of errors efficiently.
The AppError
class is the base for all errors in the package. It accepts a message, status code, and optional details.
const { AppError } = require('error-cure');
const error = new AppError('Something went wrong', 500);
console.log(error.message); // "Something went wrong"
console.log(error.statusCode); // 500
console.log(error.status); // "error"
The ValidationError
class tracks validation-specific errors.
const { ValidationError } = require('error-cure');
const error = new ValidationError('Invalid input', 'email', { expected: 'email format' });
console.log(error.message); // "Invalid input"
console.log(error.field); // "email"
console.log(error.details); // { expected: 'email format' }
The AuthError
class handles authentication-related errors.
const { AuthError } = require('error-cure');
const error = new AuthError('Authentication failed');
console.log(error.message); // "Authentication failed"
console.log(error.statusCode); // 401
The DatabaseError
class handles database-related errors and accepts an optional query string.
const { DatabaseError } = require('error-cure');
const error = new DatabaseError('Database connection failed', 'SELECT * FROM users');
console.log(error.message); // "Database connection failed"
console.log(error.query); // "SELECT * FROM users"
The global error handler middleware is designed for Express.js applications. It formats errors and sends appropriate responses.
In your app.js
or server.js
file:
const express = require('express');
const { globalErrorHandler } = require('error-cure');
const app = express();
// Example route that throws an error
app.get('/', (req, res, next) => {
const error = new Error('Something went wrong!');
error.statusCode = 500;
next(error);
});
// Global error handling middleware
app.use(globalErrorHandler);
app.listen(3000, () => {
console.log('Server running on port 3000');
});
The middleware formats and returns the error response based on whether it’s operational or not.
The logging utility writes errors to a log file for debugging.
const { logError } = require('error-cure');
try {
throw new Error('Something bad happened');
} catch (err) {
logError(err);
}
This will append the error to an error.log
file in the project root directory.
Error-Cure provides utilities to handle unhandled promise rejections and uncaught exceptions globally.
In your index.js
or app.js
file:
const { handleUnhandledRejections } = require('error-cure');
// Automatically handle unhandled promise rejections
handleUnhandledRejections();
This ensures that unhandled promise rejections are logged and processed correctly.
To ensure the package works as expected, run tests using the following command:
npm test
If you have Jest configured, run:
npx jest
Write tests for each error class and utility to maintain robustness.
We welcome contributions! Follow these steps:
- Fork the repository.
- Clone your fork.
- Create a new branch for your feature or bug fix.
- Commit your changes and push the branch.
- Submit a pull request.
Please adhere to our Code of Conduct.
Error-Cure is licensed under the MIT License. See the LICENSE file for details.
Node.js, Express, error handling, middleware, error classes, global error handling, logging, unhandled exceptions, validation errors, authentication errors.
Simplify and strengthen error handling in your Node.js and Express applications with Error-Cure. Get started today!