souq-error-handler
TypeScript icon, indicating that this package has built-in type declarations

1.1.8 • Public • Published

ErrorHandler

This project provides a customizable error handling library for Node.js applications. ErrorHandler sends alerts using Opsgenie, logs error messages to a file, and can be easily integrated into other projects.

Usage

To use the ErrorHandler in your project, first install it as a dependency:

Add the following values do your .env file

OPSGENIE_GENERIC_CHANNEL_API_KEY=your_api_key
PRODUCTION_CRITICAL_SLACK_ALERT_CHANNEL_ID=PRODUCTION_CRITICAL_SLACK_ALERT_CHANNEL_ID
LOG_FILE_PATH=/path/to/error.log
npm install --save souq-error-handler

Then, import and use the ErrorHandler in your project:

const ErrorHandler = require('errorHandler');

// Initialize ErrorHandler instance
const errorHandler = new ErrorHandler(
  process.env.OPSGENIE_GENERIC_CHANNEL_API_KEY || '',
  process.env.LOG_FILE_PATH || 'error.log'
);

// Log error to file
errorHandler.logError('Logging an example error message into the error.log');

// Send error to team devices with specified severity
errorHandler.slackAlert('Logging an example critical error message into the slack', 'critical').catch((err) => {
    console.error('A critical slack alert:', err);
});

errorHandler.slackAlert('Logging an example moderate error message into the slack', 'moderate').catch((err) => {
    console.error('A moderate slack alert:', err);
});

// Send error to team devices and send generic alert
errorHandler.genericAlert('Logging an example error directly to team devices').catch((err) => {
    console.error('A generic alert:', err);
});

Running Tests

To run tests, execute the following command:

npm test

Linting

This project uses ESLint for linting. To fix linting issues, run:

npm run lint --fix

Further Development

  1. Clone this repository:
git clone https://github.com/yourusername/errorHandler.git
  1. Install dependencies:
npm install
  1. Copy the .env.example file to create a new .env file:
cp .env.example .env
  1. Update the .env file with your API keys, recipients, and other configuration values.

  2. (optional) Run the example usage script to see what happens:

node src/example_usage.js

Folder Structure

|-- .env
|-- .env.example
|-- .eslintignore
|-- .eslintrc.json
|-- .gitignore
|-- README.md
|-- index.js
|-- package-lock.json
|-- package.json
|-- src
|   |-- errorHandler.js
|   |-- example_usage.js
|-- tests
    |-- errorHandler.test.js

Readme

Keywords

none

Package Sidebar

Install

npm i souq-error-handler

Weekly Downloads

1

Version

1.1.8

License

MIT

Unpacked Size

11 kB

Total Files

11

Last publish

Collaborators

  • oskii