is-error-service
v1.2.1
Published
A centralized error handling package for Node.js applications
Downloads
12
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 Error Classes
The package provides predefined error classes that extend a base CustomError class. You can throw these errors in your application as needed.
Available Error Classes
- AuthError: 401 Throws an unauthorized access error.
- BadRequestError: 400 Throws a bad request error.
- ForbiddenError: Throws a 403 forbidden error.
- NotFoundError: Throws a 404 not found error.
- ValidationError: Throws a validation error.
- InternalServerError: Throws an internalServerError error
import {
AuthError,
BadRequestError,
NotFoundError,
ValidationError,
CustomError,
ForbiddenError,
InternalServerError,
} from 'is-error-service';
app.get('/auth-required-route', (req, res, next) => {
// Code: 401
// Status: fail
// Message: Unauthorized access. Token is invalid or expired.
throw new AuthError();
});
app.get('/some-route1', (req, res, next) => {
// Code: 400
// Status: fail
// Message: Bad Request
throw new BadRequestError();
});
app.get('/some-route2', (req, res, next) => {
// Code: 500
// Status: error
// Message: Something went wrong
throw new CustomError({
status: 'error',
code: 500,
message: 'Something went wrong',
});
});
app.get('/some-route3', (req, res, next) => {
// Code: 403
// Status: fail
// Message: Forbidden Access
throw new ForbiddenError();
});
app.get('/some-route4', (req, res, next) => {
// Code: 404
// Status: fail
// Message: {resource} is not defined
throw new NotFoundError({ resource: 'user' });
});
app.get('/some-route5', (req, res, next) => {
// Code: 400
// Status: fail
// Message: username must not be empty.
throw new ValidationError({ message: 'username must not be empty.' });
// Code: 400
// Status: fail
// Message: Invalid Request Data
throw new ValidationError({});
});
app.get('/some-route6', (req, res, next) => {
// Code: 500
// Status: error
// Message: Fail to modify user.
throw new InternalServerError({ message: 'Fail to modify user' });
// Code: 500
// Status: error
// Message: Internal Server Error
throw new InternalServerError({});
});
- 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,
errorServiceInit,
} from 'is-error_service';
// Initialize the app
const app = express();
errorServiceInit(process.env.NODE_ENV ?? ENV.DEV.NAME);
// 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');
});