npm package: @izzy-sato/error_service
Description: A centralized error handling package for Node.js applications
Author: Izzy
Latest version: 1.1.7
Deprecated: This package has been renamed to @is/error-service

# 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:

```bash
npm install @izzy-sato/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.

## Usage

1. 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.

```js
import {
  AuthError,
  NotFoundError,
  ValidationError,
} from '@izzy-sato/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.

2. 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.

```js
import { errorHandling } from '@izzy-sato/error-service';

// Use the middleware in your Express application
app.use(errorHandling);
```

3. Logger Setup (Winston)

The package includes logging functionality using winston for both development and production environments. Errors and important logs are captured automatically.

```ts
import { logger } from '@izzy-sato/error-service';

// Use logger for custom logging
logger.info('This is an info log');
logger.error('This is an error log');
```

5. Error Constants

The package also provides constants for managing consistent error messages.

Example of ERROR and ENV Constants:

```js
import { ERROR } from '@izzy-sato/error_service';

console.log(ERROR.DEVELOPMENT.NOT_FOUND); // Logs 'Resource not found'
```

5. Full Example of Setup

Here’s a complete example of how to use the error service package in an Express app:

```js
import express from 'express';
import { errorHandling, AuthError, logger } from '@izzy-sato/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');
});
```