is-error-service
v1.2.0
Published
A centralized error handling package for Node.js applications
Downloads
340
Readme
Error Service
A centralized error handling package for Node.js applications, providing custom error classes, error handling middleware, and logging with winston
. This package simplifies error management in production and development environments.
Installation
Install the package using npm:
npm install is-error-service
Features
- Custom Error Classes: Predefined error classes for various HTTP errors like AuthError, BadRequestError, NotFoundError, etc.
- Constants: Provides ERROR for consistent error messaging.
- Winston Logger: Logging setup for both production and development environments.
- Error Handling Middleware: Centralized middleware to handle and respond to errors, tailored to the environment.
Setup
- app.js
import { errorServiceInit } from 'is-error-service';
import { ENV } from 'is-env-service';
errorServiceInit(process.env.NODE_ENV ?? ENV.DEV.NAME);
Usage
- Using Custom Error Classes
The package provides predefined error classes that extend a base CustomError class. You can throw these errors in your application as needed.
import { AuthError, NotFoundError, ValidationError } from 'is-error-service';
app.get('/auth-required-route', (req, res, next) => {
throw new AuthError(); // Will be caught by error handling middleware
});
app.get('/some-route', (req, res, next) => {
throw new NotFoundError(); // Will trigger 404 response
});
Available Error Classes
- AuthError: Throws an unauthorized access error.
- BadRequestError: Throws a 400 bad request error.
- ForbiddenError: Throws a 403 forbidden error.
- NotFoundError: Throws a 404 not found error.
- ValidationError: Throws a validation error.
- Error Handling Middleware
The error handling middleware provides different responses based on the current environment. In development, detailed error messages are shown, while in production, more generic messages are returned.
import { errorHandling } from 'is-error-service';
// Use the middleware in your Express application
app.use(errorHandling);
- Logger Setup (Winston)
The package includes logging functionality using winston for both development and production environments. Errors and important logs are captured automatically.
import { logger } from 'is-error-service';
// Use logger for custom logging
logger.info('This is an info log');
logger.error('This is an error log');
- Error Constants
The package also provides constants for managing consistent error messages.
Example of ERROR and ENV Constants:
import { ERROR } from 'is-error-service';
console.log(ERROR.DEVELOPMENT.NOT_FOUND); // Logs 'Resource not found'
- Full Example of Setup
Here’s a complete example of how to use the error service package in an Express app:
import express from 'express';
import { errorHandling, AuthError, logger } from 'is-error_service';
// Initialize the app
const app = express();
// Routes
app.get('/auth', (req, res, next) => {
throw new AuthError(); // Custom error thrown here
});
// Use the error handling middleware
app.use(errorHandling);
// Start the server
app.listen(3000, () => {
logger.info('Server running on port 3000');
});